我使用EF和Code First。我試圖將兩個實體映射到兩個不同的表。 首先,我嘗試將材質映射到表材質,然後嘗試將PlannedMaterial映射到材質和ScheduleRows。我可以將兩個實體映射到兩個不同的表嗎?
一個ScheduleRow需要在材質表中有一行的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)中使用流暢的API來做這個映射。可能嗎?怎麼樣?
我欣賞所有想法和建議。
的事實,你是顯示SQL表,有點違背代碼的概念。「 –