外键导航属性命名约定的替代品

编程入门 行业动态 更新时间:2024-10-25 23:22:30
本文介绍了外键导航属性命名约定的替代品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在使用实体框架4.1,是否有替代的命名约定导航属性。

When using Entity Framework 4.1, are there alternatives to the naming conventions for Navigation Properties?

例如,而不是这样做的:

For example instead of doing this:

public virtual MyObject MyObject { get; set; }

To be

public virtual MyObject SomeOtherName { get; set; }

更新:

在该 [ForeignKey的(OldStepId)] 和 [ForeignKey的(NewStepId)] 属性被添加,生成的SQL就变成了:

When the [ForeignKey("OldStepId")] and [ForeignKey("NewStepId")] attribute is added, the generated SQL then becomes:

{SELECT `Extent1`.`CompletedId`, `Extent1`.`OldStepId`, `Extent1`.`NewStepId`, `Extent1`.`Name`, `Extent1`.`Step_StepId`, `Extent1`.`Step_StepId1` FROM `Completed` AS `Extent1`}

其中,不存在的最后两列。

which, the last two columns do not exist.

推荐答案

您可以使用数据注释或流畅API来做到这一点。

You can use the Data Annotations or the Fluent API to do this

属性的方式

public virtual Int32 MyObjectId{get;set;} [ForeignKey("MyObjectId")] public virtual MyObject SomeOtherName { get; set; }

流利路

Fluent Way

modelBuilder.Entity<Type>() .HasRequired(p => p.SomeOtherName) .WithMany(d => d.Type) .HasForeignKey(p => p.MyObjectId)

响应更新

如果您在MyOjbect类有一个列表,那么你就需要来标记列表作为 [InverseProperty(SomeOtherName)] 。这也许就是为什么你在你的SQL获得额外列。这使得从告诉发电机所在的主柱真的是被成倍上涨的双向关系。

If you have a List in your MyOjbect class, then you need to mark that List as [InverseProperty("SomeOtherName")]. This might be why you are getting extra columns in your SQL. This keeps two-way relationships from being doubled up by telling the generator where the main column really is.

更多推荐

外键导航属性命名约定的替代品

本文发布于:2023-11-12 00:59:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1580051.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:替代品   属性

发布评论

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

>www.elefans.com

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