简体   繁体   中英

entity framework code first fluent api

I am using the fluent api for the first time . 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

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

And tableB has following fields:

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:

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. So in your case 2 entities are related if theirs tAId and tBId columns have the same value. 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:

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.

I haven't done 1:1 with the fluent API, but I have done it with attributes. 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; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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