EF4.1(代码优先)-如何指定复合关系

发布于 2024-11-19 21:59:30 字数 564 浏览 4 评论 0原文

在 Linq to SQL 中,我可以指定一个不必依赖于数据库中现有的外键和 pks 的关系,这对于创建如下所示的复合关系很有用:

public class Equipment_CableNormalised
{
    ...

    [Association(ThisKey = "EquipmentId,PortNumber", OtherKey = "EquipmentId,PortNumber", IsForeignKey = false)]
    public List<EquipmentPort> EquipmentPorts
    {
        get; set;
    }

}

然后生成类似于 " .. join EquipmentPorts 的 sql EP 上的 EP.EquipmentId = blah 和 EP.PortNumber = Blah”

我可以在 EF4.1 中做同样的事情(使用注释或 Fluent api)吗?我知道您可以指定复合键并使用 [Keys] 和 [ForeignKeys] 属性,但这种关系不会映射到键......

In Linq to SQL I could specify a relationship that didn't have to depend on the foreign keys and pks existing in the database, useful for creating composite relationships like this:

public class Equipment_CableNormalised
{
    ...

    [Association(ThisKey = "EquipmentId,PortNumber", OtherKey = "EquipmentId,PortNumber", IsForeignKey = false)]
    public List<EquipmentPort> EquipmentPorts
    {
        get; set;
    }

}

This then generated the sql similar to " .. join EquipmentPorts EP on EP.EquipmentId = blah and EP.PortNumber = Blah".

Can I do the same sort of thing in EF4.1 (using annotations or fluent api)? I know you can specify composite keys and use the [Keys] and [ForeignKeys] attributes, but this relationship doesn't map to keys...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

您的代码中的示例关系如何工作?我希望 EquipementId 一侧必须是 PK 或唯一密钥(L2S 和 EF 中均不支持),因为否则关系就无法存在(一对一和一对多都需要唯一主体)。一旦一侧出现 PK,端口号就多余了。

代码优先仅允许映射到键。如果您有现有数据库,您可以在模型中欺骗它,并以与映射现有数据库相同的方式映射新关系,但您仍然必须遵循简单的规则 - 主体中的属性是主键,依赖实体中的属性被映射为外键。

如果您希望 EF 为您生成 DB,那么您将始终拥有数据库中的所有关系。

How does the sample relation from your code works? I expect that EquipementId must be either PK or unique key (not supported in both L2S and EF) on one side because otherwise the relation could not exist (both one-to-one and one-to-many demands unique principal). Once it is PK on one side the port number is redundant.

Code first allows only mapping to keys. If you have existing database you can cheat it in your model and map new relations in the same way as you would map existing but you still have to follow simple rule - properties in principal are primary keys, properties in dependent entity are mapped as foreign keys.

If you want EF to generate DB for you, you will always have all relations in the database.

浅忆 2024-11-26 21:59:30

使用 HasKey http://www.ienablemuch。 com/2011/06/mapping-class-to-database-view-with.html

要么使用 HasKey,将其放在 OnModelCreating 上

 modelBuilder.Entity<SalesOnEachCountry>().HasKey(x => new { x.CountryId, x.OrYear });   

,要么使用 Key 列顺序

public class SalesOnEachCountry
{        
    [Key, Column(Order=0)] public int CountryId { get; set; }
    public string CountryName { get; set; }
    [Key, Column(Order=1)] public int OrYear { get; set; }
     
    public long SalesCount { get; set; }      
    public decimal TotalSales { get; set; }
}

关于您的问题关于外键,我还没有尝试过纯代码(OnModelCreating)方法,也许你可以在子类本身上放置两个ForeignKey属性,可能也需要放置Column Order。

这可能是答案 复合键作为外键

这个答案证实了我的预感,你可以在子类本身上放置两个ForeignKey属性。

Use HasKey http://www.ienablemuch.com/2011/06/mapping-class-to-database-view-with.html

Either use HasKey, put this on OnModelCreating

 modelBuilder.Entity<SalesOnEachCountry>().HasKey(x => new { x.CountryId, x.OrYear });   

Or use Key Column Order

public class SalesOnEachCountry
{        
    [Key, Column(Order=0)] public int CountryId { get; set; }
    public string CountryName { get; set; }
    [Key, Column(Order=1)] public int OrYear { get; set; }
     
    public long SalesCount { get; set; }      
    public decimal TotalSales { get; set; }
}

Regarding your question about foreign key, I haven't yet tried the pure code(OnModelCreating) approach, perhaps you can just put two ForeignKey attribute on child class itself, might need to put Column Order too.

This could be the answer composite key as foreign key

That answer confirms my hunch that you could put two ForeignKey attributes on child class itself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文