English 中文(简体)
利用实体框架绘制两个以上表格
原标题:Mapping more than two tables using the Entity Framework

我正在考虑在今后数周内开始利用第一个框架开展一个项目。

我在以前建立的数据库中有三个表格。 https://i.stack.imgur.com/Ka47J.png“alt=“EDMX”/。

创建公司Notice记录时,必须将至少一个地点(从地点表)列入公司NoticeLocations表。

e.g.

**CompanyNotice**
ID  CompanyID Date                      Heading Text   .....
2   9         2011-11-21 10:17:29.573   Lorem   Ipsum   1   1   1

**CompanyNoticeLocations**
CompanyNoticeLocationsID CompanyNoticeID LocationID
1                        2               4
2                        2               5
3                        2               1

Main question: Can anyone please tell me if I can use the EF to create an Entity called: CompanyNoticesWithLocations that just returns:

  • Heading
  • Text
  • List of LocationNames

www.un.org/Depts/DGACM/index_spanish.htm 我已尝试与LINQ一道这样做,而没有向实体制图提供多张表格,我无法做到:

using (var context = new ALEntities())
                {
                    var query = from c in context.CompanyNotices.Include("Locations")
                                select new 
                                { 
                                    c.CompanyNoticeHeading, 
                                    c.CompanyNoticeText,
                                    (from l in c.CompanyNoticesLocations select l.Location.LocationName)
                                };
                    ASPxGridView1.DataSource = query;
                    ASPxGridView1.DataBind();



                }

然而,我有了一个错误:

Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.

最佳回答

Your CompanyNoticesLocation table doesn t need to exist as a separate entity in the model. This should be represented as a many to many Association between CompanyNotice and Location. As long as the Association is setup correctly EF will map this to your underlying join table which you ll be able to see in the Mapping window.

这里的一个例子是,从我自己的模型之一绘制了许多至许多地图,显示协会绘制地图,然后把候选人Answer编成表:

enter image description here

问题回答

this works for me

var query = from c in context.CompanyNotices.Include("Locations") 
                                select new  
                                {  
                                    c.CompanyNoticeHeading,  
                                    c.CompanyNoticeText, 
                                    locations = (from l in c.CompanyNoticesLocations select l.Location.LocationName) 
                            }; 

所在地点是您匿名财产的名称。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签