简体   繁体   English

实体框架代码第一流畅的api

[英]entity framework code first fluent api

I am using the fluent api for the first time . 我是第一次使用流畅的api。 I am able to establish relationshionship using one to many and many to many relationship. 我能够利用一对多和多对多的关系来建立关系。

But I have a clarification using one-to-one relationship. 但我使用一对一的关系进行澄清。

I have two tables tableA and tableB wherein tableA has two fields 我有两个表tableA和tableB,其中tableA有两个字段

public class tableA
{
 public int tAId {get;set;}
 public string desc {get;set;}
 public tableB tableB {get;set;}
}

And tableB has following fields: 而tableB有以下字段:

public class tableB
{
  public int tBId {get;set;}
  public int refKeyfromTableA{get;set;}
  public string somedesc{get;set;}
  public tableA tableA {get;set;}

}

I am defining the constraints in a separate class like : 我在一个单独的类中定义约束,如:

public class tableAConfig:BaseEntity<tableA>
{
public tableAConfig()
{
  HasKey(p=>p.tAId);
Property(p=>p.tAId).IsRequired();

//This line has syntatical error
HasForeignKey(p=>p.tAId);
}
}

How to define the foreign key relationship in the above class in code first approach? 如何在代码第一种方法中定义上述类中的外键关系?

Define your fluent api configuration class as follows: 定义您的流畅api配置类,如下所示:

public class tableAConfig:BaseEntity<tableA>
{
    public tableAConfig()
    {
        HasKey(p=>p.tAId);

        HasOptional(p => p.tableB )
            .WithRequired( p => p.tableA );
    }
}

Take into account that property refKeyfromTableA on tableB entity is useless as one-to-one relationship in database is formed between primary keys. 考虑到tableB实体上的属性refKeyfromTableA是无用的,因为主键之间形成了数据库中的一对一关系。 So in your case 2 entities are related if theirs tAId and tBId columns have the same value. 因此,在您的情况下,如果他们的tAId和tBId列具有相同的值,则2个实体是相关的。 So values for primary key of at least one of the entities cannot be genereated by database. 因此,至少一个实体的主键值不能由数据库生成。 For instance in configuration of tableB you can do it as follows: 例如,在tableB的配置中,您可以按如下方式执行:

Property(e => e.tBId)
    .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

Apart from WithRequired method you may as well use WithOptionalDependent and WithOptionalPrincipal methods to form one-to-one relationship as you wish. 除了WithRequired方法,您还可以使用WithOptionalDependent和WithOptionalPrincipal方法来形成一对一的关系。

I haven't done 1:1 with the fluent API, but I have done it with attributes. 我还没有使用流畅的API 1:1,但我已经使用属性完成了它。 I fixed up a code sample demonstrating the attribute approach to be consistent with your example, perhaps it will be helpful to you: 我修复了一个代码示例,演示了属性方法以与您的示例保持一致,也许它对您有所帮助:

public class tableA
{
    public int Id { get; set; }

    public string desc { get; set; }

    public tableB tableB { get; set; }
}

public class tableB
{
    // In one-to-one relationship, one end must be principal and second end must be dependent. 
    // tableA is the one which will be inserted first and which can exist without the dependent one. 
    // tableB end is the one which must be inserted after the principal because it has foreign key to the principal.

    [Key, ForeignKey("tableA")]
    public int Id { get; set; }

    // 'Required' attribute because tableA must be present
    // in order for a tableB to exist
    [Required]
    public virtual tableA tableA { get; set; }

    public string somedesc { get; set; }
}

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

相关问题 实体框架4代码优先自定义表映射Fluent API问题 - Entity Framework 4 Code First Custom table mapping Fluent API issue 实体框架5.0,代码优先,关系,流利的API和播种数据库 - Entity Framework 5.0, Code First, relationships, fluent API and seeding the database 使用Fluent API的Entity Framework 6代码优先关系 - Entity Framework 6 code-first relations with Fluent API Entity Framework Code First Fluent Api:向列添加索引 - Entity Framework Code First Fluent Api: Adding Indexes to columns 实体框架6代码优先Fluent API表映射 - Entity Framework 6 Code First Fluent API Table Mapping 如何使用 Entity Framework Code First Fluent API 指定表名 - How to specify table name with Entity Framework Code First Fluent API 首先具有Fluent API并发性的实体框架代码`DbUpdateConcurrencyException`不引发 - Entity Framework Code First with Fluent API Concurrency `DbUpdateConcurrencyException` Not Raising 实体框架-代码优先/ Fluent API-过滤的导航属性 - Entity Framework - Code First / Fluent API - Filtered navigation properties 实体框架代码优先Fluent映射 - Entity Framework code first Fluent mapping 用于一对一实体关系的Entity Framework 6代码第一个流利的api配置 - Entity Framework 6 code first fluent api config for one to one entity relationship
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM