简体   繁体   English

首先使用实体​​框架代码-如何定义自定义导航属性

[英]Entity Framework code first - How to define a custom navigation property

I'm using ASP.NET core and Entity Framework 我正在使用ASP.NET核心和实体框架

I have a pre-existing database schema, and I want to add a linked table using a foreign key, however the foreign key index column isn't something that adheres to any of the standard EF conventions. 我有一个预先存在的数据库架构,我想使用一个外键添加一个链接表,但是外键索引列并不符合任何标准EF约定。 How can I express this in Ef? 我该如何在Ef中表达这一点? I'd prefer to use Data Annotation attributes if possible, but perfectly fine to use the ModelBuilder fluent API if not. 如果可能的话,我宁愿使用Data Annotation属性,但如果没有,最好使用ModelBuilder fluent API。

Essentially this: 本质上是这样的:

[Table("widgets")]
class Widget {
  [Key, Column("id")]
  int Id { get; set; }

  [Column("referencenumber")]
  int ReferenceNumber { get; set; }

  public WidgetReferenceData ReferenceData // ???? How do I express this?
}

[Table("widgetreferences")]
class WidgetReference {
  [Key, Column("referencenumber")]
  int ReferenceNumber { get; set; }

  [Column("value")]      
  string Value { get;set; }
}

In the above model, A Widget may have zero or one associated entry in the WidgetReference table, linked using the widgets.referencenumber database column. 在上述模型中,在WidgetReference表中,一个Widget可能具有零个或一个相关条目,并使用widgets.referencenumber数据库列进行链接。

What I'd like to do is add a property to my Widget model to go and load this associated WidgetReference (or null if there's not one present). 我想做的是向我的Widget模型中添加一个属性,以加载此关联的WidgetReference(如果不存在,则为null)。 Basically I want to be able to use it like this: 基本上,我希望能够像这样使用它:

var referenceValue = FindWidget().ReferenceData?.Value;

If it were a conventional "Id" column, EF would take care of it, but as I have custom database column and property names, how do I express this relation? 如果它是常规的“ Id”列,则EF会处理它,但是由于我具有自定义数据库列和属性名,该如何表达这种关系?

I'm not sure it affects the outcome, but it's critical that widgetreferences.referencenumber is NOT a foreign key, but rather just a column with an index on it. 我不确定它是否会影响结果,但至关重要的是, widgetreferences.referencenumber不是外键,而只是带有索引的列。 Our business logic is such that if a Widget gets deleted, we still want to retain the reference data 我们的业务逻辑是,如果删除了某个小部件,我们仍然希望保留参考数据

I've tried and failed to find the answer to this via Googling, but it feels like something that shouldn't be hard :-( 我试图通过谷歌搜索找不到答案,但是感觉有些不难:-(

    [Table("widgets")]
    class Widget 
   {
      [Key, Column("id")]
      int Id { get; set; }

      [Column("referencenumber")]
      int? ReferenceNumber { get; set; }

      [ForeignKey("ReferenceNumber")]
      public virtual WidgetReferenceData ReferenceData // ???? How do I express this?
    }

    [Table("widgetreferences")]
    class WidgetReference 
    {
      [Key, Column("referencenumber")]
      int ReferenceNumber { get; set; }

      [Column("value")]      
      string Value { get;set; }
    }

Just use the ForeignKeyAttribute to associate the Navigation Property with the Foreign Key Property. 只需使用ForeignKeyAttribute将Navigation属性与Foreign Key属性相关联即可。 EF doesn't care if there is actually a Foreign Key in the back-end. EF不在乎后端中是否确实有外键。

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

相关问题 如何通过实体框架代码优先方法定义导航属性 - How to define a navigation property via Entity Framework code first approach 实体框架代码优先 IQueryable 导航属性 - Entity Framework code-first IQueryable navigation property 实体框架代码第一个子导航属性null - Entity Framework Code First Child Navigation Property null 具有实体框架代码优先导航属性的ASP.Net MVC - ASP.Net MVC with Entity Framework Code First Navigation Property 实体框架代码优先,不同上下文/数据库之间的导航属性 - Entity Framework Code First, Navigation Property between different contexts/databases 通过关系表的实体框架代码优先导航属性 - Entity Framework Code First navigation property through relational table C# Entity Framework 6 Code First:如何为存储在 DB 中的属性定义特定的 setter - C# Entity Framework 6 Code First: how to define a specific setter for a property stored in DB 如何首先在Entity Framework Code中加载导航数据? - How to load navigation data in Entity framework Code first? EF代码优先如何使用Entity检索导航属性? - EF code-first how to retrieve navigation-property with Entity? 实体框架 5(代码优先)导航属性 - Entity Framework 5 (Code First) Navigation Properties
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM