实体框架代码优先:1:0..1更改外键位置

编程入门 行业动态 更新时间:2024-10-23 20:24:45
本文介绍了实体框架代码优先:1:0..1更改外键位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在实体框架代码优先模型中定义了1-to-0..1关系:

I have a 1-to-0..1 relationship defined in an Entity Framework code first model like this:

public class Album { public int AlbumId { get; set; } public int StampId { get; set; } public Stamp Stamp { get; set; } // other properties } public class Stamp { public int StampId { get; set; } public int AlbumId { get; set; } [Required] public Album Album { get; set; } // other properties }

所以..一张专辑有0..1个图章,一个图章始终只有一个专辑.我在这里的配置效果很好.但是,当我查看数据库中生成的列时,我有点不满意:外键是在Album表中创建的.这使您很难/缓慢地批量插入新图章.始终需要更改Album表并在那里更新StampId外键. (这意味着我需要进行更改跟踪才能更改这些字段)

So.. an album has 0..1 stamps, a stamp always has exactly one album. The configuration I have here works nicely. However when I look at what columns are generated in the data base, I'm a bit unhappy: The foreign key is created in the Album table.. which makes it a hard/slow to bulk-insert new Stamps, as you always need to alter the Album table and update the StampId Foreign Keys there. (That means I need change tracking to change those fields)

如何告诉Entity Framework在Stamp表中创建外键?

How can I tell Entity Framework to create the foreign key in the Stamp table?

我也不知道导航属性的声明在这种情况下起什么作用.是否在两个方向上都定义了这些属性是否重要?

I'm also not sure what role the declaration of the navigation properties play in this context.. does it matter whether you have those properties defined in both directions?

推荐答案

好,我使用在这里找到的漂亮示例弄清楚了: www.entityframeworktutorial/code-first/configure一对一关系-code-first.aspx

Ok, I figured it out using the nice examples I found here: www.entityframeworktutorial/code-first/configure-one-to-one-relationship-in-code-first.aspx

技巧是使用邮票"表中的"AlbumID"外键作为主键.因此,这意味着戳记ID"将不是主键,并且主键将具有针对不存在的ID的间隙".因此,换言之,您可以确保一张专辑仅拥有一张邮票. 由于此概念有点令人讨厌,因此仍然可以模拟一个UID"StampID",只要您添加新条目,它就会递增.

The trick is to use the 'AlbumID' foreign key in the 'Stamps' table as primary key. So that implies that the Stamp Ids will not be the primary key, and that the primary key will have 'gaps' for the IDs which do not exist. So in other words, by doing that you are guarantee that one Album has only one Stamp. Since this concept is a bit irritating, one can still simulate a UID 'StampID' which increments whenever you add a new entry.

因此在我的示例中将是:

So in my example that would be:

public class Album { public int AlbumId { get; set; } public Stamp Stamp { get; set; } // other properties } public class Stamp { [Index(IsUnique = true)] // another UID, just to follow the naming pattern [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int StampId { get; set; } [Key, ForeignKey("Album")] // The PK, taken as FK from the associated Album public int AlbumId { get; set; } [Required] // the required attribute just makes validation errors more readable public Album Album { get; set; } // other properties }

更多推荐

实体框架代码优先:1:0..1更改外键位置

本文发布于:2023-11-05 09:54:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1560530.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:键位   实体   框架   代码

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!