C# C-实体框架-使用外键插入数据

C# C-实体框架-使用外键插入数据,c#,linq,entity-framework,C#,Linq,Entity Framework,这是数据库架构: CREATE TABLE Recipe ( id int not null identity(1,1) primary key, title varchar(140) not null, preparationTime int not null, cookingTime int not null, difficulty varchar(25) not null, picture varchar(140) not null, nbStars float not null, nbP

这是数据库架构:

CREATE TABLE Recipe (
id int not null identity(1,1) primary key,
title varchar(140) not null,
preparationTime int not null,
cookingTime int not null,
difficulty varchar(25) not null,
picture varchar(140) not null,
nbStars float not null,
nbPersons int not null,
description varchar(5000) not null,
ingredientsDescription varchar(800) not null,
mailFacebook varchar(60) not null
);

ALTER TABLE Recipe
ADD Constraint preparationTimeLessThan0 CHECK(preparationTime > 0);

ALTER TABLE Recipe
ADD Constraint cookingTimeLessThan0 CHECK(cookingTime > 0);

ALTER TABLE Recipe
ADD Constraint nbStarsLessThan0 CHECK(nbStars >= 0 AND nbStars < 6);

ALTER TABLE Recipe
ADD Constraint nbPersonsLessThan1 CHECK(nbPersons > 0);

CREATE TABLE Ingredient (
id int not null identity(1,1) primary key,
name varchar(50) not null,
htmlName varchar(70) not null,
refCategory int not null,
FOREIGN KEY(refCategory) REFERENCES Category
);

CREATE TABLE IngredientRecipe (
idRecipe int not null,
idIngredient int not null,
PRIMARY KEY(idRecipe,idIngredient),
FOREIGN KEY(idRecipe) REFERENCES Recipe,
FOREIGN KEY(idIngredient) REFERENCES Ingredient
);
当我在数据库中添加菜谱时,我的菜谱记录得很好,但我没有适合我的菜谱的正确成分。然而,在我做更改之前,当我打印IngredCenter中包含的不同配方的不同成分时,成分是正确的

示例:我在打印配方的一组配料之前将其保存到数据库中 id:6088姓名:茄子 身份证号码:6094姓名:卡洛特斯 id:6068姓名:huile d'olive id:6077姓名:波弗尔 id:6384姓名:孜然

在IngredEntercipe表的数据库中,当我打印配方的不同成分时,我得到了这个。idIngredient类似于自动递增属性。我不明白为什么:

 idRecipe   idIngredient
   1341        8777
   1341        8778
   1341        8779
   1341        8780
   1341        8781
你有什么解决办法吗


谢谢

您的实体配置在哪里?你是先用代码的吗?数据库优先?在我的projet/Models/IDB.edmx中。我还在我的项目/Models/IDB.edmx中添加了一条指令,以防止IDB.context.tti中的循环引用。我还添加了一条指令来防止IDB.context.tt中的循环引用。什么是代码优先?数据库优先?对于每个食谱,在进行保存更改之前,我打印了每个食谱的成分,这些成分都是正确的。但是在执行SaveChanges之后,我注意到在我的数据库中有一个问题。代码优先和数据库优先是将数据实体映射到数据库的两种方法。了解你是如何告诉它食谱和成分是相关的,这将真正有助于找出它们为什么不起作用。
 idRecipe   idIngredient
   1341        8777
   1341        8778
   1341        8779
   1341        8780
   1341        8781