简体   繁体   English

实体框架映射联接表字段

[英]Entity Framework Mapping join table fields

I have a join table that has a field in it that I need to get into an Entity and have it updatable. 我有一个联接表,其中有一个字段,我需要进入一个实体并使其可更新。 I have the Table setup below, the column I need is " PersonelleID " in the " Account " table. 我有如下表设置,我需要的列是“ PersonelleID ”中的“ Account ”表。 Now, there may be multiple, so in that case, there's a concept of primary (think of it as if you were a student who went from one school to another, you'd have the same account but multiple schools). 现在,可能有多个学校,所以在这种情况下,有一个小学的概念(考虑一下,就好像您是一个从一所学校转到另一所学校的学生一样,您拥有相同的账户但有多所学校)。

Any idea how I can bring this into the Entity world? 知道如何将其带入实体世界吗? Generating the database ignores this field on the join table (probably because it doesn't know where to put it). 生成数据库会忽略联接表上的该字段(可能是因为它不知道将其放在何处)。

Trying to see what the best route to go is. 尝试看看最好的路线是什么。

实体问题

if you are using the Code-First approach you would create an entity for the join table that has foreign keys to both Account and School and your extra properties. 如果您使用的是“代码优先”方法,则将为连接表创建一个实体,该实体具有同时指向Account和School以及您的其他属性的外键。

public class Account
{
    public int AccountId { get; set; }
    public string UserName { get; set;}
}

public class AccountSchool
{
    [ForeignKey("Account")]
    public int AccountId { get; set; }

    [ForeignKey("School")]
    public string CEEBCodeId { get; set; }

    public string PersonelleID { get; set; }
}

public class School
{
    [Key]
    public string CEEBCodeId { get; set; }
    public string Name { get; set;}
}

This is how I've done it and here's is an article explaining how to do it as well: http://www.itq.nl/blogs/post/Code-First-Entity-Framework-Additional-properties-on-many-to-many-join-tables.aspx 这就是我的操作方法,下面是一篇文章也介绍了操作方法: http : //www.itq.nl/blogs/post/Code-First-Entity-Framework-Additional-properties-on-many -to-to-join-tables.aspx

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

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