简体   繁体   English

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

[英]Entity Framework Code First: 1:0..1 Change Foreign Key Location

I have a 1-to-0..1 relationship defined in an Entity Framework code first model like this: 我在实体框架代码优先模型中定义了1-to-0..1关系,如下所示:

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
}

So.. an album has 0..1 stamps, a stamp always has exactly one album. 所以..一张专辑有0..1个图章,一个图章总是只有一个专辑。 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. 但是,当我查看数据库中生成的列时,我有点不满意:外键是在“ Album表中创建的。这使您很难/缓慢地批量插入新的图章。更改Album表并在那里更新StampId外键。 (That means I need change tracking to change those fields) (这意味着我需要进行更改跟踪才能更改这些字段)

How can I tell Entity Framework to create the foreign key in the Stamp table? 如何告诉Entity Framework在Stamp表中创建外键?

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? 我也不确定导航属性的声明在此上下文中起什么作用。.是否在两个方向上都定义了这些属性是否重要?

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 好的,我使用在这里找到的漂亮示例弄清楚了: http : //www.entityframeworktutorial.net/code-first/configure-one-to-one-relationship-in-code-first.aspx

The trick is to use the 'AlbumID' foreign key in the 'Stamps' table as primary key . 诀窍是使用“邮票”表中的“ AlbumID”外键作为主键 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. 因此,这意味着“戳记ID”将不是主键,并且主键将具有针对不存在的ID的“间隙”。 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. 由于这个概念有点烦人,因此您仍然可以模拟一个UID'StampID',只要您添加新条目,该ID就会递增。

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
}

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

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