【发布时间】:2016-04-15 12:54:43
【问题描述】:
我正在使用 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 modelBuilder) 中的 fluent API 进行此映射。是否可以?怎么样?
感谢所有想法和建议。
【问题讨论】:
-
你显示 SQL 表的事实,有点违背代码优先的概念。¨
标签: c# entity-framework ef-code-first