Aca*_*uza 7 .net entity-framework data-annotations entity-framework-4.1
我有一个使用Entity Framework 4.1的关系,我的外键是自动生成的,外键的名称有下划线:
public class Setor : Entity
{
public long Id { get; set; }
public string Nome { get; set; }
public virtual Secretaria Secretaria { get; set; }
}
public class Secretaria : Entity
{
public long Id { get; set; }
public string Nome { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这在表Setor中生成了名为:Secretaria_Id的外键
我想删除这个下划线:SecretariaId
有没有办法做到这一点?我更喜欢使用DataAnnotations.
在Fluent API中,您可以为FK列指定名称:
modelBuilder.Entity<Setor>()
.HasOptional(s => s.Secretaria)
.WithMany()
.Map(a => a.MapKey("SecretariaId"));
Run Code Online (Sandbox Code Playgroud)
我认为DataAnnotations无法做到这一点.或者,您可以将外键公开到模型类中,如下所示:
public class Setor : Entity
{
public long Id { get; set; }
public string Nome { get; set; }
public long? SecretariaId { get; set; }
public virtual Secretaria Secretaria { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
约定将自动识别,因为FK和列名称将是属性的名称,即SecretariaId.
| 归档时间: |
|
| 查看次数: |
3086 次 |
| 最近记录: |