我正在使用EF和Code First。我尝试将两个实体映射到两个不同的表。 首先,我尝试将Material映射到表Materials,然后我尝试将PlannedMaterial映射到Materials和ScheduleRows。
一个ScheduleRow需要在Materials表中有一行Foreigtn键。从Materials表到ScheduleRows表没有外键。
这些是我的实体。
public class Material
{
public int Id { get; set; } // from material table
public string Name { get; set; } // from material table
// public PlannedMaterial PlannedMaterial { get; set; } Do I need this? I don't want a relation from this side.
}
public class PlannedMaterial
{
public int Id { get; set; } // from *ScheduleRow table*
public string Name { get; set; } // from *material table*
public int SequenceNo { get; set; } // from *ScheduleRow* table
public Material Material { get; set; } // Do I need this? I'm only interrested in the Name column from the material table
}
这些是我的桌子。
CREATE TABLE [dbo].[RunScheduleRow](
[Id] [int] IDENTITY(1,1) NOT NULL,
[SequenceNo] [int] NOT NULL,
[Material_id] [int] NOT NULL) -- FK to Material table
CREATE TABLE [dbo].[Material](
[Id] [int] NOT NULL,
[Name] [nvarchar](max) NULL)
我想在OnModelCreating(DbModelBuilder模型构建器)中使用流畅的API进行映射。可能吗?怎么样?
我感谢所有的想法和建议。
答案 0 :(得分:0)
我认为你的设计并不好。您尝试管理ScheduleRow和Material Tables之间的关系,但这种关系可以由实体框架管理。
public class Material
{
public int Id { get; set; } // from material table
public string Name { get; set; } // from material table
public ICollection<ScheduleRow> ScheduleRows{ get; set; }
}
public class ScheduleRow
{
public int Id { get; set; } // from *ScheduleRow table*
public int SequenceNo { get; set; } // from *ScheduleRow* table
public Material Material { get; set; }
}
配置你的shcema
public class YourContext: DbContext
{
public SchoolDBContext(): base()
{
}
public DbSet<Material> Materials { get; set; }
public DbSet<ScheduleRow> ScheduleRows { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//Configure default schema
modelBuilder.HasDefaultSchema("YourShema");
}
}
将实体映射到表
命名空间CodeFirst_FluentAPI_Tutorials {
public class YourContext: DbContext
{
public YourDBContext(): base()
{
}
public DbSet<Material> Materials { get; set; }
public DbSet<ScheduleRow> ScheduleRows { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//Configure default schema
modelBuilder.HasDefaultSchema("YourSchema");
//Map entity to table
modelBuilder.Entity<Material>().ToTable("Material");
modelBuilder.Entity<ScheduleRow>().ToTable("RunScheduleRow","dbo");
}
}
}
要配置列名称,请将此指令添加到上下文
modelBuilder.Entity<YourEntity>()
.Property(p => p.propertyname)
.HasColumnName("Caolum_name")
.HasColumnOrder(your_column_order)
.HasColumnType("column_type");
希望对你有所帮助。
答案 1 :(得分:0)
在您跳到解决方案之前,请阅读本文,它将阐明EF实体的用户身份,因为我可以向您承诺,这不是您认为的。
EF规则:
那你错过了什么? 你是我的好先生,缺少规则1,所以你必须建立一个软实体(常规DTO /模型/类)
有点像这样。但请注意,现在PlannedMaterial不会保存在任何地方。public class PlannedMaterial
{
public PlannedMaterial(ScheduleRow s, Material m){
ScheduleRow = s;
Material = m;
}
public int Id { get{return _s.Id; } set{_s.Id = value;} }
public string Name { get{return _m.Name; } set{_m.Name = value; }
public int SequenceNo { get{return _s.SequenceNo; } set{_s.SequenceNo = value; }
Material _m{ get; set; }
ScheduleRow _s{ get; set; }
}
以下是如何处理您的实体
public class Material
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<ScheduleRow> ScheduleRows { get; set; }
}
public class ScheduleRow
{
public int Id { get; set; }
public int SequenceNo { get; set; }
public virtual Material Material { get; set; }
}
上下文(参见构建器)
public class YourContext: DbContext
{
public SchoolDBContext(): base()
{
}
public DbSet<Material> Materials { get; set; }
public DbSet<ScheduleRow> ScheduleRows { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//Fluent API
modelBuilder.Entity<Material>()
.HasMany(e => e.ScheduleRow)
.WithRequired(e => e.Material)
.HasForeignKey(e => e.Id)
.WillCascadeOnDelete(false);
base.OnModelCreating(modelBuilder);
}
}
链接: FluentAPI