Code First中的关系 - CTP4 [英] Relation in Code First - CTP4

查看:76
本文介绍了Code First中的关系 - CTP4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


 


我有以下型号:


 public class Pais 
{
public int Id {get;组; }
public string Nome {get;组; }
public string CodigoIBGE {get;组; }

public virtual ICollection< Uf> Ufs {get;组; }
}

公共类PaisConfiguration:EntityConfiguration< Pais>
{
public PaisConfiguration()
{
this.MapSingleType(t => new
{
id = t.Id,
nome = t.Nome,
codigoibge = t.CodigoIBGE
})。ToTable(new StoreTableName(" pais"," tabfixa"));

this.HasKey(t => t.Id);
}
}

公共类Uf
{
public string Id {get;组; }
public string Nome {get;组; }
public int PaisId {get;组; }

public virtual Pais Pais {get;组; }
}


公共类UfConfiguration:EntityConfiguration< Uf>
{
public UfConfiguration()
{
this.MapSingleType(t => new
{
id = t.Id,
nome = t.Nome,
id_pais = t.PaisId
})。ToTable(new StoreTableName(" uf"," tabfixa"));

this.HasKey(t => t.Id);
}
}



 
public class FixaContext:DbContext
{

public FixaContext()
base ()
{
}

protected 覆盖 void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add( new PaisConfiguration());
modelBuilder.Configurations.Add( new UfConfiguration());
}

public DbSet< Pais> tPais { get ; set ; }
public DbSet< Uf> tUf { get ; set ; }
}

解决方案

Rogerio,


 


MapSingleType中存在一个错误,导致FK无法与您的关系正确关联。 
我们将解决此问题。  目前,解决方法是在您的一个配置文件中明确配置您的关系。 
例如,将以下内容添加到UfConfiguration构造函数中:


 


this 。HasRequired(u = > u.Pais).WithMany(p => p.Ufs).HasConstraint((u,p)=> u.PaisId
== p.Id);


 


谢谢,


Arthur



Hello all,

 

I have the following model:

 public class Pais
  {
    public int Id { get; set; }
    public string Nome { get; set; }
    public string CodigoIBGE { get; set; }

    public virtual ICollection<Uf> Ufs { get; set; }
  }

  public class PaisConfiguration : EntityConfiguration<Pais>
  {
    public PaisConfiguration()
    {
      this.MapSingleType(t => new
      {
        id = t.Id,
        nome = t.Nome,
        codigoibge = t.CodigoIBGE
      }).ToTable(new StoreTableName("pais", "tabfixa"));

      this.HasKey(t => t.Id);
    }
  }

  public class Uf
  {
    public string Id { get; set; }
    public string Nome { get; set; }
    public int PaisId { get; set; }

    public virtual Pais Pais { get; set; }
  }


  public class UfConfiguration : EntityConfiguration<Uf>
  {
    public UfConfiguration()
    {
      this.MapSingleType(t => new
      {
        id = t.Id,
        nome = t.Nome,
        id_pais = t.PaisId
      }).ToTable(new StoreTableName("uf", "tabfixa"));

      this.HasKey(t => t.Id);
    }
  }

And

  public class FixaContext : DbContext
  {    
    
    public FixaContext()
      : base()
    {
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
      modelBuilder.Configurations.Add(new PaisConfiguration());
      modelBuilder.Configurations.Add(new UfConfiguration());
    }

    public DbSet<Pais> tPais { get; set; }
    public DbSet<Uf> tUf { get; set; }
  }

解决方案

Rogerio,

 

It appears that there is a bug in MapSingleType that is causing the FK not to be correctly associated with your relationship.  We’ll fix this.  For now, the workaround is to explicitly configure your relationship in one of your configuration files.  For example, add the following to the UfConfiguration constructor:

 

this.HasRequired(u => u.Pais).WithMany(p => p.Ufs).HasConstraint((u, p) => u.PaisId == p.Id);

 

Thanks,

Arthur


这篇关于Code First中的关系 - CTP4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆