简体   繁体   English

如何在Asp net Mvc中合并三个表?

[英]How to merge three tables in Asp net Mvc?

I have created three tables in the sql and then I have merged this three tables together to view the all records in each table.我在sql中创建了三个表,然后将这三个表合并在一起以查看每个表中的所有记录。 And I have used to created this code in MVC Error has been displayed below.我曾经在MVC中创建此代码错误已显示在下面。

The data reader is incompatible with the specified 'MedeilMVC_CLOUD.Models.Company'.数据读取器与指定的“MedeilMVC_CLOUD.Models.Company”不兼容。 A member of the type, 'CompanyID', does not have a corresponding column in the data reader with the same name类型为“CompanyID”的成员在数据读取器中没有对应的同名列

错误

//View Company
public ActionResult ViewCompany()
{
    var data = dp.Company.SqlQuery("select CompanyName,ShortName,Division,ContactPerson,Designation,Email,Address1,Address2,Pincode,Country,State,City,MobileNo,PhoneNo,PanNo,TinNo,GSTno,IECCode,C.CountryName,s.StateName,C.CountryCode,C.DialingCode from CompanyRegistration AS CR LEFT JOIN VAN_SETTING.[dbo].[Country] AS C ON CR.Country=C.CountryID LEFT JOIN VAN_SETTING.[dbo].[State] AS S ON CR.State=S.StateID ORDER BY CompanyID  DESC").ToList();
    return View(data);
}

Below Image下图

错误

The Error message indicates the model you are reading into, has a Property named CompanyID.错误消息表明您正在读入的模型具有名为 CompanyID 的属性。

I have added CompanyID in the select below, see how that goes.我在下面的选择中添加了 CompanyID,看看效果如何。

//View Company
public ActionResult ViewCompany()
{
    var data = dp.Company.SqlQuery("select CompanyID, CompanyName,ShortName,Division,ContactPerson,Designation,Email,Address1,Address2,Pincode,Country,State,City,MobileNo,PhoneNo,PanNo,TinNo,GSTno,IECCode,C.CountryName,s.StateName,C.CountryCode,C.DialingCode from CompanyRegistration AS CR LEFT JOIN VAN_SETTING.[dbo].[Country] AS C ON CR.Country=C.CountryID LEFT JOIN VAN_SETTING.[dbo].[State] AS S ON CR.State=S.StateID ORDER BY CompanyID  DESC").ToList();
    return View(data);
}

You can try the bellow code.你可以试试下面的代码。 After join you need to specify the key for relation.加入后,您需要指定关系的键。

  var data = dp.Company.SqlQuery("select CompanyName,ShortName,Division,ContactPerson,Designation,Email,Address1,Address2,Pincode,Country,State,City,MobileNo,PhoneNo,PanNo,TinNo,GSTno,IECCode,C.CountryName,s.StateName,C.CountryCode,C.DialingCode from CompanyRegistration AS CR LEFT JOIN VAN_SETTING.[dbo].[Country] AS C ON CR.Country.Id=C.CountryID LEFT JOIN VAN_SETTING.[dbo].[State] AS S ON CR.State.Id=S.StateID ORDER BY CompanyID  DESC").ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM