实体框架代码优先:1:0..1更改外键位置 [英] Entity Framework Code First: 1:0..1 Change Foreign Key Location

查看:258
本文介绍了实体框架代码优先: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?

推荐答案

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

Ok, I figured it out using the nice examples I found here: http://www.entityframeworktutorial.net/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更改外键位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆