EndTimeStamp = DateTime.Now, RequestType =" r", StartTimeStamp = DateTime.Now, Status =" s", StatusDescription =" sd" }; e.AddToConversationSet(c); var m = new Messages() { AccountUid = Guid.NewGuid()。ToString(), Body =" body", DataCenter =" 3", ExchangeId =" 1", MessageTimeStamp = DateTime.Now, ParentId =" 2", RequestType =" reqt",&retryCount = 3 , RouteFrom =" rf", RouteFromBroker =" rfb", RouteTo =" rt" }; m。会话= c; e.AddToMessagesSet(m); var mh = new MessageHeaders() { Name Name =" name", Value =" value" }; mh.Messages = m; e.AddToMessageHeadersSet(mh); 试试{ e.SavingChanges + = new EventHandler(e_SavingChanges); e.SaveChanges(); scope.Complete(); } catch(Exception ex) {{br / > string s ="" ;; } } <?xml version =" 1.0" encoding =" utf-8"?> < edmx:Edmx Version =" 1.0" xmlns:edmx =" http://schemas.microsoft.com/ado/2007/06/edmx"> <! - EF运行时内容 - > < edmx:运行时> <! - SSDL内容 - > < edmx:StorageModels> < Schema Namespace =" TrackerModel.Store"别名= QUOT;自"提供商= QUOT; System.Data.SqlClient的" ProviderManifestToken = QUOT; 2008"的xmlns:商店= QUOT; HTTP://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns =" http://schemas.microsoft.com/ado/2006/04/edm/ssdl"> < EntityContainer Name =" TrackerModelStoreContainer"> < EntitySet Name = "会话与QUOT;的EntityType = QUOT; TrackerModel.Store.Conversation"商店:种类="表"架构= QUOT; DBO" /> < EntitySet Name =" MessageHeaders"的EntityType = QUOT; TrackerModel.Store.MessageHeaders"商店:种类="表"架构= QUOT; DBO" /> < EntitySet Name =" Messages"的EntityType = QUOT; TrackerModel.Store.Messages"商店:种类="表"架构= QUOT; DBO" /> < AssociationSet Name =" FK_MessageHeaders_Messages" Association =" TrackerModel.Store.FK_MessageHeaders_Messages"> < End Role =" Messages" EntitySet的= QUOT;消息" /> < End Role =" MessageHeaders" EntitySet的= QUOT; MessageHeaders" /> < / AssociationSet> < AssociationSet Name =" FK_Messages_Conversation" Association =" TrackerModel.Store.FK_Messages_Conversation"> < End Role =" Conversation" EntitySet的= QUOT;会话与QUOT; /> < End Role =" Messages" EntitySet的= QUOT;消息" /> < / AssociationSet> < / EntityContainer> < EntityType Name =" Conversation"> < Key> < PropertyRef Name =" ConversationId" /> < / Key> < Property Name =" ConversationId"类型=" VARCHAR"可空= QUOT假QUOT;的MaxLength = QUOT; 255" /> < Property Name =" Status"类型=" VARCHAR"的MaxLength = QUOT; 50" /> < Property Name =" RequestType"类型=" VARCHAR"的MaxLength = QUOT; 255" /> < Property Name =" StatusDescription"类型="文本" /> < Property Name =" StartTimeStamp"类型="日期时间"可空= QUOT假QUOT; /> < Property Name =" EndTimeStamp"类型="日期时间" /> < / EntityType> < EntityType Name =" MessageHeaders"> < Key> < PropertyRef Name =" ExchangeId" /> < PropertyRef Name =" Name" /> < / Key> < Property Name =" ExchangeId"类型=" VARCHAR"可空= QUOT假QUOT;的MaxLength = QUOT; 255" /> < Property Name =" Name"类型=" VARCHAR"可空= QUOT假QUOT;的MaxLength = QUOT; 255" /> < Property Name =" Value"类型=" VARCHAR(最大)" /> < / EntityType> < EntityType Name =" Messages"> < Key> < PropertyRef Name =" ExchangeId" /> < / Key> < Property Name =" ExchangeId"类型=" VARCHAR"可空= QUOT假QUOT;的MaxLength = QUOT; 255" /> < Property Name =" ParentId"类型=" VARCHAR"的MaxLength = QUOT; 255" /> < Property Name =" ConversationId"类型=" VARCHAR"可空= QUOT假QUOT;的MaxLength = QUOT; 255" /> < Property Name =" DataCenter"类型=" VARCHAR"的MaxLength = QUOT; 25" /> < Property Name =" AccountUid"类型=" VARCHAR"的MaxLength = QUOT; 50" /> < Property Name =" RetryCount"类型=" INT"可空= QUOT假QUOT; /> < Property Name =" RouteFrom"类型=" VARCHAR"可空= QUOT假QUOT;的MaxLength = QUOT; 255" /> < Property Name =" RouteFromBroker"类型=" VARCHAR"可空= QUOT假QUOT;的MaxLength = QUOT; 255" /> < Property Name =" RouteTo"类型=" VARCHAR"可空= QUOT假QUOT;的MaxLength = QUOT; 255" /> < Property Name =" RequestType"类型=" VARCHAR"的MaxLength = QUOT; 255" /> < Property Name =" Body"类型="文本" /> < Property Name =" MessageTimeStamp"类型="日期时间"可空= QUOT假QUOT; /> < / EntityType> < Association Name =" FK_MessageHeaders_Messages"> < End Role =" Messages"类型=" TrackerModel.Store.Messages"多重= QUOT 1 QUOT; /> < End Role =" MessageHeaders"类型=" TrackerModel.Store.MessageHeaders"多重= QUOT * QUOT; /> < ReferentialConstraint> < Principal Role =" Messages"> < PropertyRef Name =" ExchangeId" /> < / Principal> < Dependent Role =" MessageHeaders"> < PropertyRef Name =" ExchangeId" /> < / Dependent> < / ReferentialConstraint> < / Association> < Association Name =" FK_Messages_Conversation"> < ;结束角色="对话"类型=" TrackerModel.Store.Conversation"多重= QUOT 1 QUOT; /> < End Role =" Messages"类型=" TrackerModel.Store.Messages"多重= QUOT * QUOT; /> < ReferentialConstraint> < Principal Role =" Conversation"> < PropertyRef Name =" ConversationId" /> < / Principal> < Dependent Role =" Messages"> < PropertyRef Name =" ConversationId" /> < / Dependent> < / ReferentialConstraint> < / Association> < / Schema>< / edmx:StorageModels> <! - CSDL内容 - > < edmx:ConceptualModels> < Schema Namespace =" TrackerModel"别名= QUOT;自" xmlns =" http://schemas.microsoft.com/ado/2006/04/edm"> < EntityContainer Name =" TrackerEntities"> < EntitySet Name =" ConversationSet"的EntityType = QUOT; TrackerModel.Conversation" /> < EntitySet Name =" MessageHeadersSet"的EntityType = QUOT; TrackerModel.MessageHeaders" /> < EntitySet Name =" MessagesSet"的EntityType = QUOT; TrackerModel.Messages" /> < AssociationSet Name =" FK_Messages_Conversation" Association =" TrackerModel.FK_Messages_Conversation"< End Role =" Conversation" EntitySet的= QUOT; ConversationSet" /> < End Role =" Messages" EntitySet的= QUOT; MessagesSet" /> < / AssociationSet> < AssociationSet Name =" FK_MessageHeaders_Messages" Association =" TrackerModel.FK_MessageHeaders_Messages"> < End Role =" Messages" EntitySet的= QUOT; MessagesSet" /> < End Role =" MessageHeaders" EntitySet的= QUOT; MessageHeadersSet" /> < / AssociationSet> < / EntityContainer> < EntityType Name =" Conversation"> < Key> < PropertyRef Name =" ConversationId" /> < / Key> < Property Name =" ConversationId"类型="字符串"可空= QUOT假QUOT;的MaxLength = QUOT; 255"的Unicode = QUOT假QUOT;定长= QUOT假QUOT; /> < Property Name =" Status"类型="字符串"的MaxLength = QUOT; 50"的Unicode = QUOT假QUOT;定长= QUOT假QUOT; /> < Property Name =" RequestType"类型="字符串"的MaxLength = QUOT; 255"的Unicode = QUOT假QUOT;定长= QUOT假QUOT; /> < Property Name =" StartTimeStamp"类型=" DateTime的"可空= QUOT假QUOT; /> < Property Name =" EndTimeStamp"类型=" DateTime的" /> < NavigationProperty Name =" Messages"关系= QUOT; TrackerModel.FK_Messages_Conversation" FromRole = QUOT;会话与QUOT; ToRole = QUOT;消息" /> < Property Name =" StatusDescription"类型="字符串"可空= QUOT;真" />< / EntityType> < EntityType Name =" MessageHeaders"> < Key> < PropertyRef Name =" ExchangeId" /> < PropertyRef Name =" Name" /> < / Key> < Property Name =" ExchangeId"类型="字符串"可空= QUOT假QUOT;的MaxLength = QUOT; 255"的Unicode = QUOT假QUOT;定长= QUOT假QUOT; /> < Property Name =" Name"类型="字符串"可空= QUOT假QUOT;的MaxLength = QUOT; 255"的Unicode = QUOT假QUOT;定长= QUOT假QUOT; /> < Property Name =" Value"类型="字符串"可空= QUOT假QUOT;的MaxLength = QUOT;最大"的Unicode = QUOT假QUOT;定长= QUOT假QUOT; /> < NavigationProperty Name =" Messages"关系= QUOT; TrackerModel.FK_MessageHeaders_Messages" FromRole = QUOT; MessageHeaders" ToRole = QUOT;消息" /> < / EntityType> < EntityType Name =" Messages"> < Key> < PropertyRef Name =" ExchangeId" /> < / Key> < Property Name =" ExchangeId"类型="字符串"可空= QUOT假QUOT;的MaxLength = QUOT; 255"的Unicode = QUOT假QUOT;定长= QUOT假QUOT; /> < Property Name =" ParentId"类型="字符串"的MaxLength = QUOT; 255"的Unicode = QUOT假QUOT;定长= QUOT假QUOT; /> < Property Name =" DataCenter"类型="字符串"的MaxLength = QUOT; 25"的Unicode = QUOT假QUOT;定长= QUOT假QUOT; /> < Property Name =" AccountUid"类型="字符串"的MaxLength = QUOT; 50"的Unicode = QUOT假QUOT;定长= QUOT假QUOT; /> < Property Name =" RetryCount"类型="&的Int32 QUOT;可空= QUOT假QUOT; /> < Property Name =" RouteFrom"类型="字符串"可空= QUOT假QUOT;的MaxLength = QUOT; 255"的Unicode = QUOT假QUOT;定长= QUOT假QUOT; /> < Property Name =" RouteFromBroker"类型="字符串"可空= QUOT假QUOT;的MaxLength = QUOT; 255"的Unicode = QUOT假QUOT;定长= QUOT假QUOT; /> < Property Name =" RouteTo"类型="字符串"可空= QUOT假QUOT;的MaxLength = QUOT; 255"的Unicode = QUOT假QUOT;定长= QUOT假QUOT; /> < Property Name =" RequestType"类型="字符串"的MaxLength = QUOT; 255"的Unicode = QUOT假QUOT;定长= QUOT假QUOT; /> < Property Name =" Body"类型="字符串"的MaxLength = QUOT;最大"的Unicode = QUOT假QUOT;定长= QUOT假QUOT; /> < Property Name =" MessageTimeStamp"类型=" DateTime的"可空= QUOT假QUOT; /> < NavigationProperty Name =" Conversation"关系= QUOT; TrackerModel.FK_Messages_Conversation" FromRole = QUOT;消息" ToRole = QUOT;会话与QUOT; /> < NavigationProperty Name =" MessageHeaders"关系= QUOT; TrackerModel.FK_MessageHeaders_Messages" FromRole = QUOT;消息" ToRole = QUOT; MessageHeaders" /> < / EntityType> < Association Name =" FK_Messages_Conversation"> < End Role =" Conversation"类型=" TrackerModel.Conversation"多重= QUOT 1 QUOT; /> < End Role =" Messages"类型=" TrackerModel.Messages"多重= QUOT * QUOT; /> < / Association> < Association Name =" FK_MessageHeaders_Messages"> < End Role =" Messages"类型=" TrackerModel.Messages"多重= QUOT 1 QUOT; /> < End Role =" MessageHeaders"类型=" TrackerModel.MessageHeaders"多重= QUOT * QUOT; /> < ReferentialConstraint> < Principal Role =" Messages"> < PropertyRef Name =" ExchangeId" />< / Principal> < Dependent Role =" MessageHeaders"> < PropertyRef Name =" ExchangeId" />< / Dependent>< / ReferentialConstraint>< / Association> < / Schema> < / edmx:ConceptualModels> <! - CS映射内容 - > < edmx:Mappings> < Mapping Space =" C-S" xmlns =" urn:schemas-microsoft-com:windows:storage:mapping:CS"> < EntityContainerMapping StorageEntityContainer =" TrackerModelStoreContainer" CdmEntityContainer =" TrackerEntities"> < EntitySetMapping Name =" ConversationSet"> < EntityTypeMapping TypeName =" IsTypeOf(TrackerModel.Conversation)"> < MappingFragment StoreEntitySet =" Conversation"< ScalarProperty Name =" StatusDescription"的ColumnName = QUOT;状态说明" /> < ScalarProperty Name =" ConversationId"的ColumnName = QUOT;&的conversationId QUOT; /> < ScalarProperty Name =" Status"的ColumnName = QUOT;状态" /> < ScalarProperty Name =" RequestType"的ColumnName = QUOT;&的RequestType QUOT; /> < ScalarProperty Name =" StartTimeStamp"的ColumnName = QUOT; StartTimeStamp" /> < ScalarProperty Name =" EndTimeStamp"的ColumnName = QUOT; EndTimeStamp" /> < / MappingFragment> < / EntityTypeMapping> < / EntitySetMapping> < EntitySetMapping Name =" MessageHeadersSet"> < ; EntityTypeMapping TypeName =" IsTypeOf(TrackerModel.MessageHeaders)"> < MappingFragment StoreEntitySet =" MessageHeaders"> < ScalarProperty Name =" ExchangeId"的ColumnName = QUOT;交换ID" /> < ScalarProperty Name =" Name"的ColumnName = QUOT;姓名" /> < ScalarProperty Name =" Value"的ColumnName = QUOT;值" /> < / MappingFragment> < / EntityTypeMapping> < / EntitySetMapping> < EntitySetMapping Name =" MessagesSet"> < ; EntityTypeMapping TypeName =" IsTypeOf(TrackerModel.Messages)">< MappingFragment StoreEntitySet =" Messages"> < ScalarProperty Name =" ExchangeId"的ColumnName = QUOT;交换ID" /> < ScalarProperty Name =" ParentId"的ColumnName = QUOT;的ParentId" /> < ScalarProperty Name =" DataCenter"的ColumnName = QUOT;数据中心与QUOT; /> < ScalarProperty Name =" AccountUid"的ColumnName = QUOT; AccountUid" /> < ScalarProperty Name =" RetryCount"的ColumnName = QUOT; RetryCount重" /> < ScalarProperty Name =" RouteFrom"的ColumnName = QUOT; RouteFrom" /> < ScalarProperty Name =" RouteFromBroker"的ColumnName = QUOT; RouteFromBroker" /> < ScalarProperty Name =" RouteTo"的ColumnName = QUOT; RouteTo" /> < ScalarProperty Name =" RequestType"的ColumnName = QUOT;&的RequestType QUOT; /> < ScalarProperty Name =" Body"的ColumnName = QUOT;机体" /> < ScalarProperty Name =" MessageTimeStamp"的ColumnName = QUOT; MessageTimeStamp" /> < / MappingFragment> < / EntityTypeMapping> < / EntitySetMapping> < AssociationSetMapping Name =" FK_Messages_Conversation"类型名= QUOT; TrackerModel.FK_Messages_Conversation" StoreEntitySet =" Messages"> < EndProperty Name =" Conversation"> < ScalarProperty Name =" ConversationId"的ColumnName = QUOT;&的conversationId QUOT; /> < / EndProperty> < EndProperty Name =" Messages"> < ScalarProperty Name =" ExchangeId"的ColumnName = QUOT;交换ID" /> < / EndProperty> < / AssociationSetMapping> < AssociationSetMapping Name =" FK_MessageHeaders_Messages"类型名= QUOT; TrackerModel.FK_MessageHeaders_Messages" StoreEntitySet =" MessageHeaders"> < EndProperty Name =" Messages"> < ScalarProperty Name =" ExchangeId"的ColumnName = QUOT;交换ID" /> < / EndProperty> < EndProperty Name =" MessageHeaders"> < ScalarProperty Name =" ExchangeId"的ColumnName = QUOT;交换ID" /> < ScalarProperty Name =" Name"的ColumnName = QUOT;姓名" /> < / EndProperty> < / AssociationSetMapping> < / EntityContainerMapping> < / mapping> < / edmx:Mappings> < / edmx:运行时> <! - EF Designer内容(请勿在此手动编辑) - > < edmx:Designer xmlns =" http: //schemas.microsoft.com/ado/2007/06/edmx"> < edmx:Connection> < DesignerInfoPropertySet> < DesignerProperty Name =" MetadataArtifactProcessing"值= QUOT; EmbedInOutputAssembly" /> < / DesignerInfoPropertySet> < / edmx:Connection> < edmx:Options> < DesignerInfoPropertySet> < DesignerProperty Name = " ValidateOnBuild"值= QUOT;真" /> < / DesignerInfoPropertySet> < / edmx:选项> <! - 图表内容(形状和连接器位置) - > < edmx:Diagrams> < Diagram Name =" TrackerModel"> < EntityTypeShape EntityType =" TrackerModel.Conversation"宽度= QUOT; 1.5" PointX = QUOT; 0.75"尖= QUOT; 1.5"高度= QUOT; 1.9210009765625002" IsExpanded = QUOT;真" /> < EntityTypeShape EntityType =" TrackerModel.MessageHeaders"宽度= QUOT; 1.5" PointX = QUOT; 5.875"尖= QUOT; 1.625"高度= QUOT; 1.592306315104167" IsExpanded = QUOT;真" /> < EntityTypeShape EntityType =" TrackerModel.Messages"宽度= QUOT; 1.5" PointX = QUOT; 3英寸尖= QUOT 1 QUOT;高度= QUOT; 3.0714322916666661" IsExpanded = QUOT;真" /> < AssociationConnector Association =" TrackerModel.FK_Messages_Conversation" ManuallyRouted =" false"> < ConnectorPoint PointX =" 2.25"尖= QUOT; 2.5426741536458337" /> < ConnectorPoint PointX =" 3"尖= QUOT; 2.5426741536458337" />< / AssociationConnector> < AssociationConnector Association =" TrackerModel.FK_MessageHeaders_Messages" ManuallyRouted =" false"> < ConnectorPoint PointX =" 4.5"尖= QUOT; 2.4211531575520837" /> < ConnectorPoint PointX =" 5.875"尖= QUOT; 2.4211531575520837" /> < / AssociationConnector> < / Diagram>< / edmx:Diagrams> < / edmx:Designer> < / edmx:Edmx>Whenever I attempt to insert into all tables and submit a single SaveChanges it throws an exception that I am breaking the FK constraint.  Here is my code.  Also the Sql Profile confirms that it is trying to insert into conversation and then messageheaders instead of conversation, messages, messageheaders.  Is there a way to force insert order here? [TestMethod]         public void TransactionalInsertTest()         {             using (var scope = new TransactionScope())             {                 var e = new TrackerEntities();                 var c = new Conversation()                 {                     ConversationId = "1",                     EndTimeStamp = DateTime.Now,                     RequestType = "r",                     StartTimeStamp = DateTime.Now,                     Status = "s",                     StatusDescription = "sd"                 };                 e.AddToConversationSet(c);                 var m = new Messages()                 {                     AccountUid = Guid.NewGuid().ToString(),                     Body = "body",                     DataCenter = "3",                     ExchangeId = "1",                     MessageTimeStamp = DateTime.Now,                     ParentId = "2",                     RequestType = "reqt",                     RetryCount = 3,                     RouteFrom = "rf",                     RouteFromBroker = "rfb",                     RouteTo = "rt"                 };                 m.Conversation = c;                 e.AddToMessagesSet(m);                 var mh = new MessageHeaders()                 {                     Name = "name",                     Value = "value"                 };                 mh.Messages = m;                 e.AddToMessageHeadersSet(mh);                 try                 {                     e.SavingChanges += new EventHandler(e_SavingChanges);                     e.SaveChanges();                     scope.Complete();                 }                 catch (Exception ex)                 {                     string s = "";                 }             }         } <?xml version="1.0" encoding="utf-8"?> <edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx">   <!-- EF Runtime content -->   <edmx:Runtime>     <!-- SSDL content -->     <edmx:StorageModels>     <Schema Namespace="TrackerModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2008" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2006/04/edm/ssdl">         <EntityContainer Name="TrackerModelStoreContainer">           <EntitySet Name="Conversation" EntityType="TrackerModel.Store.Conversation" store:Type="Tables" Schema="dbo" />           <EntitySet Name="MessageHeaders" EntityType="TrackerModel.Store.MessageHeaders" store:Type="Tables" Schema="dbo" />           <EntitySet Name="Messages" EntityType="TrackerModel.Store.Messages" store:Type="Tables" Schema="dbo" />           <AssociationSet Name="FK_MessageHeaders_Messages" Association="TrackerModel.Store.FK_MessageHeaders_Messages">             <End Role="Messages" EntitySet="Messages" />             <End Role="MessageHeaders" EntitySet="MessageHeaders" />           </AssociationSet>           <AssociationSet Name="FK_Messages_Conversation" Association="TrackerModel.Store.FK_Messages_Conversation">             <End Role="Conversation" EntitySet="Conversation" />             <End Role="Messages" EntitySet="Messages" />           </AssociationSet>         </EntityContainer>         <EntityType Name="Conversation">           <Key>             <PropertyRef Name="ConversationId" />           </Key>           <Property Name="ConversationId" Type="varchar" Nullable="false" MaxLength="255" />           <Property Name="Status" Type="varchar" MaxLength="50" />           <Property Name="RequestType" Type="varchar" MaxLength="255" />           <Property Name="StatusDescription" Type="text" />           <Property Name="StartTimeStamp" Type="datetime" Nullable="false" />           <Property Name="EndTimeStamp" Type="datetime" />         </EntityType>         <EntityType Name="MessageHeaders">           <Key>             <PropertyRef Name="ExchangeId" />             <PropertyRef Name="Name" />           </Key>           <Property Name="ExchangeId" Type="varchar" Nullable="false" MaxLength="255" />           <Property Name="Name" Type="varchar" Nullable="false" MaxLength="255" />           <Property Name="Value" Type="varchar(max)" />         </EntityType>         <EntityType Name="Messages">           <Key>             <PropertyRef Name="ExchangeId" />           </Key>           <Property Name="ExchangeId" Type="varchar" Nullable="false" MaxLength="255" />           <Property Name="ParentId" Type="varchar" MaxLength="255" />           <Property Name="ConversationId" Type="varchar" Nullable="false" MaxLength="255" />           <Property Name="DataCenter" Type="varchar" MaxLength="25" />           <Property Name="AccountUid" Type="varchar" MaxLength="50" />           <Property Name="RetryCount" Type="int" Nullable="false" />           <Property Name="RouteFrom" Type="varchar" Nullable="false" MaxLength="255" />           <Property Name="RouteFromBroker" Type="varchar" Nullable="false" MaxLength="255" />           <Property Name="RouteTo" Type="varchar" Nullable="false" MaxLength="255" />           <Property Name="RequestType" Type="varchar" MaxLength="255" />           <Property Name="Body" Type="text" />           <Property Name="MessageTimeStamp" Type="datetime" Nullable="false" />         </EntityType>         <Association Name="FK_MessageHeaders_Messages">           <End Role="Messages" Type="TrackerModel.Store.Messages" Multiplicity="1" />           <End Role="MessageHeaders" Type="TrackerModel.Store.MessageHeaders" Multiplicity="*" />           <ReferentialConstraint>             <Principal Role="Messages">               <PropertyRef Name="ExchangeId" />             </Principal>             <Dependent Role="MessageHeaders">               <PropertyRef Name="ExchangeId" />             </Dependent>           </ReferentialConstraint>         </Association>         <Association Name="FK_Messages_Conversation">           <End Role="Conversation" Type="TrackerModel.Store.Conversation" Multiplicity="1" />           <End Role="Messages" Type="TrackerModel.Store.Messages" Multiplicity="*" />           <ReferentialConstraint>             <Principal Role="Conversation">               <PropertyRef Name="ConversationId" />             </Principal>             <Dependent Role="Messages">               <PropertyRef Name="ConversationId" />             </Dependent>           </ReferentialConstraint>         </Association>       </Schema></edmx:StorageModels>     <!-- CSDL content -->     <edmx:ConceptualModels>       <Schema Namespace="TrackerModel" Alias="Self" xmlns="http://schemas.microsoft.com/ado/2006/04/edm">         <EntityContainer Name="TrackerEntities">           <EntitySet Name="ConversationSet" EntityType="TrackerModel.Conversation" />           <EntitySet Name="MessageHeadersSet" EntityType="TrackerModel.MessageHeaders" />           <EntitySet Name="MessagesSet" EntityType="TrackerModel.Messages" />           <AssociationSet Name="FK_Messages_Conversation" Association="TrackerModel.FK_Messages_Conversation">             <End Role="Conversation" EntitySet="ConversationSet" />             <End Role="Messages" EntitySet="MessagesSet" />           </AssociationSet>           <AssociationSet Name="FK_MessageHeaders_Messages" Association="TrackerModel.FK_MessageHeaders_Messages">             <End Role="Messages" EntitySet="MessagesSet" />             <End Role="MessageHeaders" EntitySet="MessageHeadersSet" />           </AssociationSet>           </EntityContainer>         <EntityType Name="Conversation">           <Key>             <PropertyRef Name="ConversationId" />           </Key>           <Property Name="ConversationId" Type="String" Nullable="false" MaxLength="255" Unicode="false" FixedLength="false" />           <Property Name="Status" Type="String" MaxLength="50" Unicode="false" FixedLength="false" />           <Property Name="RequestType" Type="String" MaxLength="255" Unicode="false" FixedLength="false" />           <Property Name="StartTimeStamp" Type="DateTime" Nullable="false" />           <Property Name="EndTimeStamp" Type="DateTime" />           <NavigationProperty Name="Messages" Relationship="TrackerModel.FK_Messages_Conversation" FromRole="Conversation" ToRole="Messages" />           <Property Name="StatusDescription" Type="String" Nullable="true" /></EntityType>         <EntityType Name="MessageHeaders">           <Key>             <PropertyRef Name="ExchangeId" />             <PropertyRef Name="Name" />             </Key>           <Property Name="ExchangeId" Type="String" Nullable="false" MaxLength="255" Unicode="false" FixedLength="false" />           <Property Name="Name" Type="String" Nullable="false" MaxLength="255" Unicode="false" FixedLength="false" />           <Property Name="Value" Type="String" Nullable="false" MaxLength="Max" Unicode="false" FixedLength="false" />           <NavigationProperty Name="Messages" Relationship="TrackerModel.FK_MessageHeaders_Messages" FromRole="MessageHeaders" ToRole="Messages" />           </EntityType>         <EntityType Name="Messages">           <Key>             <PropertyRef Name="ExchangeId" />           </Key>           <Property Name="ExchangeId" Type="String" Nullable="false" MaxLength="255" Unicode="false" FixedLength="false" />           <Property Name="ParentId" Type="String" MaxLength="255" Unicode="false" FixedLength="false" />           <Property Name="DataCenter" Type="String" MaxLength="25" Unicode="false" FixedLength="false" />           <Property Name="AccountUid" Type="String" MaxLength="50" Unicode="false" FixedLength="false" />           <Property Name="RetryCount" Type="Int32" Nullable="false" />           <Property Name="RouteFrom" Type="String" Nullable="false" MaxLength="255" Unicode="false" FixedLength="false" />           <Property Name="RouteFromBroker" Type="String" Nullable="false" MaxLength="255" Unicode="false" FixedLength="false" />           <Property Name="RouteTo" Type="String" Nullable="false" MaxLength="255" Unicode="false" FixedLength="false" />           <Property Name="RequestType" Type="String" MaxLength="255" Unicode="false" FixedLength="false" />           <Property Name="Body" Type="String" MaxLength="Max" Unicode="false" FixedLength="false" />           <Property Name="MessageTimeStamp" Type="DateTime" Nullable="false" />           <NavigationProperty Name="Conversation" Relationship="TrackerModel.FK_Messages_Conversation" FromRole="Messages" ToRole="Conversation" />           <NavigationProperty Name="MessageHeaders" Relationship="TrackerModel.FK_MessageHeaders_Messages" FromRole="Messages" ToRole="MessageHeaders" />           </EntityType>         <Association Name="FK_Messages_Conversation">           <End Role="Conversation" Type="TrackerModel.Conversation" Multiplicity="1" />           <End Role="Messages" Type="TrackerModel.Messages" Multiplicity="*" />         </Association>         <Association Name="FK_MessageHeaders_Messages">           <End Role="Messages" Type="TrackerModel.Messages" Multiplicity="1" />           <End Role="MessageHeaders" Type="TrackerModel.MessageHeaders" Multiplicity="*" />           <ReferentialConstraint>             <Principal Role="Messages">               <PropertyRef Name="ExchangeId" /></Principal>             <Dependent Role="MessageHeaders">               <PropertyRef Name="ExchangeId" /></Dependent></ReferentialConstraint></Association>         </Schema>     </edmx:ConceptualModels>     <!-- C-S mapping content -->     <edmx:Mappings>       <Mapping Space="C-S" xmlns="urn:schemas-microsoft-com:windows:storage:mapping:CS">         <EntityContainerMapping StorageEntityContainer="TrackerModelStoreContainer" CdmEntityContainer="TrackerEntities">           <EntitySetMapping Name="ConversationSet">             <EntityTypeMapping TypeName="IsTypeOf(TrackerModel.Conversation)">               <MappingFragment StoreEntitySet="Conversation">                 <ScalarProperty Name="StatusDescription" ColumnName="StatusDescription" />                 <ScalarProperty Name="ConversationId" ColumnName="ConversationId" />                 <ScalarProperty Name="Status" ColumnName="Status" />                 <ScalarProperty Name="RequestType" ColumnName="RequestType" />                 <ScalarProperty Name="StartTimeStamp" ColumnName="StartTimeStamp" />                 <ScalarProperty Name="EndTimeStamp" ColumnName="EndTimeStamp" />               </MappingFragment>             </EntityTypeMapping>           </EntitySetMapping>           <EntitySetMapping Name="MessageHeadersSet">             <EntityTypeMapping TypeName="IsTypeOf(TrackerModel.MessageHeaders)">               <MappingFragment StoreEntitySet="MessageHeaders">                 <ScalarProperty Name="ExchangeId" ColumnName="ExchangeId" />                 <ScalarProperty Name="Name" ColumnName="Name" />                 <ScalarProperty Name="Value" ColumnName="Value" />               </MappingFragment>             </EntityTypeMapping>           </EntitySetMapping>           <EntitySetMapping Name="MessagesSet">             <EntityTypeMapping TypeName="IsTypeOf(TrackerModel.Messages)">               <MappingFragment StoreEntitySet="Messages">                 <ScalarProperty Name="ExchangeId" ColumnName="ExchangeId" />                 <ScalarProperty Name="ParentId" ColumnName="ParentId" />                 <ScalarProperty Name="DataCenter" ColumnName="DataCenter" />                 <ScalarProperty Name="AccountUid" ColumnName="AccountUid" />                 <ScalarProperty Name="RetryCount" ColumnName="RetryCount" />                 <ScalarProperty Name="RouteFrom" ColumnName="RouteFrom" />                 <ScalarProperty Name="RouteFromBroker" ColumnName="RouteFromBroker" />                 <ScalarProperty Name="RouteTo" ColumnName="RouteTo" />                 <ScalarProperty Name="RequestType" ColumnName="RequestType" />                 <ScalarProperty Name="Body" ColumnName="Body" />                 <ScalarProperty Name="MessageTimeStamp" ColumnName="MessageTimeStamp" />               </MappingFragment>             </EntityTypeMapping>           </EntitySetMapping>           <AssociationSetMapping Name="FK_Messages_Conversation" TypeName="TrackerModel.FK_Messages_Conversation" StoreEntitySet="Messages">             <EndProperty Name="Conversation">               <ScalarProperty Name="ConversationId" ColumnName="ConversationId" />             </EndProperty>             <EndProperty Name="Messages">               <ScalarProperty Name="ExchangeId" ColumnName="ExchangeId" />             </EndProperty>             </AssociationSetMapping>           <AssociationSetMapping Name="FK_MessageHeaders_Messages" TypeName="TrackerModel.FK_MessageHeaders_Messages" StoreEntitySet="MessageHeaders">             <EndProperty Name="Messages">               <ScalarProperty Name="ExchangeId" ColumnName="ExchangeId" />             </EndProperty>             <EndProperty Name="MessageHeaders">               <ScalarProperty Name="ExchangeId" ColumnName="ExchangeId" />               <ScalarProperty Name="Name" ColumnName="Name" />             </EndProperty>           </AssociationSetMapping>           </EntityContainerMapping>       </Mapping>     </edmx:Mappings>   </edmx:Runtime>   <!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) -->   <edmx:Designer xmlns="http://schemas.microsoft.com/ado/2007/06/edmx">     <edmx:Connection>       <DesignerInfoPropertySet>         <DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly" />       </DesignerInfoPropertySet>     </edmx:Connection>     <edmx:Options>       <DesignerInfoPropertySet>         <DesignerProperty Name="ValidateOnBuild" Value="true" />       </DesignerInfoPropertySet>     </edmx:Options>     <!-- Diagram content (shape and connector positions) -->     <edmx:Diagrams>       <Diagram Name="TrackerModel">         <EntityTypeShape EntityType="TrackerModel.Conversation" Width="1.5" PointX="0.75" PointY="1.5" Height="1.9210009765625002" IsExpanded="true" />         <EntityTypeShape EntityType="TrackerModel.MessageHeaders" Width="1.5" PointX="5.875" PointY="1.625" Height="1.592306315104167" IsExpanded="true" />         <EntityTypeShape EntityType="TrackerModel.Messages" Width="1.5" PointX="3" PointY="1" Height="3.0714322916666661" IsExpanded="true" />         <AssociationConnector Association="TrackerModel.FK_Messages_Conversation" ManuallyRouted="false">           <ConnectorPoint PointX="2.25" PointY="2.5426741536458337" />           <ConnectorPoint PointX="3" PointY="2.5426741536458337" /></AssociationConnector>         <AssociationConnector Association="TrackerModel.FK_MessageHeaders_Messages" ManuallyRouted="false">           <ConnectorPoint PointX="4.5" PointY="2.4211531575520837" />           <ConnectorPoint PointX="5.875" PointY="2.4211531575520837" />           </AssociationConnector>         </Diagram></edmx:Diagrams>   </edmx:Designer> </edmx:Edmx>推荐答案 Hi strmshdw, 我已经仔细搜索了你的edmx文件好几天了。我发现你修改了设计器中的外键。 首先,FK_MessageHeaders_Messages的外键。 在数据库中,它位于Messages中的ExchangeId和MessageHeade中的ExchangeId之间rs。 < Association 名称 = " FK_MessageHeaders_Messages " ><Association Name="FK_MessageHeaders_Messages"> < End 角色 = " 消息" 类型 = " TrackerModel.Store.Messages " 多重性 = " 1 " />  <End Role="Messages" Type="TrackerModel.Store.Messages" Multiplicity="1" /> < End 角色 = " MessageHeaders " 类型 = " TrackerModel.Store.MessageHeaders " Multiplicity = " * " />  <End Role="MessageHeaders" Type="TrackerModel.Store.MessageHeaders" Multiplicity="*" /> < ReferentialConstraint > < Principal 角色 = " 消息" >    <Principal Role="Messages">      <PropertyRef Name="ExchangeId" />      <PropertyRef Name="ExchangeId" />    </Principal>    <Dependent Role="MessageHeaders">    <Dependent Role="MessageHeaders">      <PropertyRef Name="ExchangeId" />      <PropertyRef Name="ExchangeId" />    </Dependent>  </ReferentialConstraint></Association>(From SSDL)In the model it is between ExchangeId and Name in Messages and ExchangeId in MessageHeaders. <AssociationSetMapping Name="FK_MessageHeaders_Messages" TypeName="TrackerModel.FK_MessageHeaders_Messages" StoreEntitySet="MessageHeaders"><AssociationSetMapping Name="FK_MessageHeaders_Messages" TypeName="TrackerModel.FK_MessageHeaders_Messages" StoreEntitySet="MessageHeaders">  <EndProperty Name="Messages">  <EndProperty Name="Messages">    <ScalarProperty Name="ExchangeId" ColumnName="ExchangeId" />    <ScalarProperty Name="ExchangeId" ColumnName="ExchangeId" />  </EndProperty>  <EndProperty Name="MessageHeaders">  <EndProperty Name="MessageHeaders">    <ScalarProperty Name="ExchangeId" ColumnName="ExchangeId" />    <ScalarProperty Name="ExchangeId" ColumnName="ExchangeId" />    <ScalarProperty Name="Name" ColumnName="Name" />    <ScalarProperty Name="Name" ColumnName="Name" />  </EndProperty></AssociationSetMapping>(Form C-S mapping)Second, there is a little wield that you have double primary key in MessageHeaders. <EntityType Name="MessageHeaders"><EntityType Name="MessageHeaders">  <Key>    <PropertyRef Name="ExchangeId" />    <PropertyRef Name="ExchangeId" />    <PropertyRef Name="Name" />    <PropertyRef Name="Name" />  </Key>  <Property Name="ExchangeId" Type="varchar" Nullable="false" MaxLength="255" />  <Property Name="ExchangeId" Type="varchar" Nullable="false" MaxLength="255" />  <Property Name="Name" Type="varchar" Nullable="false" MaxLength="255" />  <Property Name="Name" Type="varchar" Nullable="false" MaxLength="255" />  <Property Name="Value" Type="varchar(max)" />  <Property Name="Value" Type="varchar(max)" /></EntityType>(From SSDL)In my SQL Server, if I set double primary key in MessageHeader, I can’t set a foreign key constraint between ExchangeId in Messages and ExchangeId in MessageHeaders science ExchangeId is not unique.Also, I can’t set a oreign key constraint between ExchangeId and Name in Messages and ExchangeId in MessageHeaders since the column count is not compatible. If I’ve misunderstood you, please correct me. So I create three tables which are most like yours. The data diagram is as follows and the script is at the bottom.  With this Structure, you can get EDM like this:You can manually modify the relationship between MessageHeaders and Messages to 1 to 1. But this do not affect the following updating we do .In Entity Framework, it provides a convenient updating way with foreignkey constraints which avoids the updating order.             ThreeHeritEntities1 context = new ThreeHeritEntities1();            ThreeHeritEntities1 context = new ThreeHeritEntities1();             Conversation conversation = new Conversation();            Conversation conversation = new Conversation();            conv ersation.ConversationId = 2;            conversation.ConversationId = 2;            conversation.Status = "New";            conversation.Status = "New";             Messages message = new Messages();            Messages message = new Messages();            message.ExchangeId = 2 ;            message.ExchangeId = 2;            message.Conversation = conversation;            message.Conversation = conversation;             MessageHeaders messageHeader = new MessageHeaders();            MessageHeaders messageHeader = new MessageHeaders();            //You do not need to set the ExchangeId he re, since we define            //the message.ExchangeId as pricipal in the EDMX file.            //The record will be 2 in messageHeader.            //messageHeader.ExchangeId = 3;            messageHeader.Name = "Party";            messageHeader.Name = "Party";            messageHeader.Messages = message;            messageHeader.Messages = message;             context.AddToMessageHeaders(messageHeader);            context.AddToMessageHeaders(messageHeader);            context.SaveChanges()            context.SaveChanges()  Does this works for you? If you have any questions or concerns, please update the thread and we will have a further discussion. Best RegardsYichun Feng  The SQL sciript:/****** Object:  Table [dbo].[Conversation]    Script Date: 08/26/2009 10:42:41 ******//****** Object:  Table [dbo].[Conversation]    Script Date: 08/26/2009 10:42:41 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOSET ANSI_PADDING ONGOCREATE TABLE [dbo].[Conversation](CREATE TABLE [dbo].[Conversation](    [ConversationId] [int] NOT NULL,    [ConversationId] [int] NOT NULL,    [Status] [varchar](50) NULL,    [Status] [varchar](50) NULL, CONSTRAINT [PK_Conversation] PRIMARY KEY CLUSTERED CONSTRAINT [PK_Conversation] PRIMARY KEY CLUSTERED(    [ConversationId] ASC    [ConversationId] ASC)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY])WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]) ON [PRIMARY]) ON [PRIMARY]GOSET ANSI_PADDING OFFGO/****** Object:  Table [dbo].[Messages]    Script Date: 08/26/2009 10:42:41 ******//****** Object:  Table [dbo].[Messages]    Script Date: 08/26/2009 10:42:41 ******/SET ANSI_NULLS ON GOSET QUOTED_IDENTIFIER ONGOSET ANSI_PADDING ONGOCREATE TABLE [dbo].[Messages](CREATE TABLE [dbo].[Messages](    [ExchangeId] [int] NOT NULL,    [ExchangeId] [int] NOT NULL,    [ConversationId] [int] NULL,    [ConversationId] [int] NULL,    [Body] [varchar](50) NULL,    [Body] [varchar](50) NULL, CONSTRAINT [PK_Messages] PRIMARY KEY CLUSTERED CONSTRAINT [PK_Messages] PRIMARY KEY CLUSTERED(    [ExchangeId] ASC    [ExchangeId] ASC)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY])WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]) ON [PRIMARY]) ON [PRIMARY]GOSET ANSI_PADDING OFFGO/****** Object:  Table [dbo].[MessageHeaders]    Script Date: 08/26/2009 10:42:41 ******//****** Object:  Table [dbo].[MessageHeaders]    Script Date: 08/26/2009 10:42:41 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOSET ANSI_PADDING ONGOCREATE TABLE [dbo].[MessageHeaders](CREATE TABLE [dbo].[MessageHeaders](    [ExchangeId] [int] NOT NULL,    [ExchangeId] [int] NOT NULL,    [Name] [varchar](50) NULL,    [Name] [varchar](50) NULL, CONSTRAINT [PK_MessageHeaders] PRIMARY KEY CLUSTERED CONSTRAINT [PK_MessageHeaders] PRIMARY KEY CLUSTERED(    [ExchangeId] ASC    [ExchangeId] ASC)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY])WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]) ON [PRIMARY]) ON [PRIMARY]GOSET ANSI_PADDING OFFGO/****** Object:  ForeignKey [FK_MessageHeaders_Messages]    Script Date: 08/26/2009 10:42:41 ******//****** Object:  ForeignKey [FK_MessageHeaders_Messages]    Script Date: 08/26/2009 10:42:41 ******/ALTER TABLE [dbo].[MessageHeaders]  WITH CHECK ADD  CONSTRAINT [FK_MessageHeaders_Messages] FOREIGN KEY([ExchangeId])ALTER TABLE [dbo].[MessageHeaders]  WITH CHECK ADD  CONSTRAINT [FK_MessageHeaders_Messages] FOREIGN KEY([ExchangeId])REFERENCES [dbo].[Messages] ([ExchangeId])REFERENCES [dbo].[Messages] ([ExchangeId])GOALTER TABLE [dbo].[MessageHeaders] CHECK CONSTRAINT [FK_MessageHeaders_Messages]ALTER TABLE [dbo].[MessageHeaders] CHECK CONSTRAINT [FK_MessageHeaders_Messages]GO/****** Object:  ForeignKey [FK_Messages_Conversation]    Script Date: 08/26/2009 10:42:41 ******//****** Object:  ForeignKey [FK_Messages_Conversation]    Script Date: 08/26/2009 10:42:41 ******/ALTER TABLE [dbo].[Messages]  WITH CHECK ADD  CONSTRAINT [FK_Messages_Conversation] FOREIGN KEY([ConversationId])ALTER TABLE [dbo].[Messages]  WITH CHECK ADD  CONSTRAINT [FK_Messages_Conversation] FOREIGN KEY([ConversationId])REFERENCES [dbo].[Conversation] ([ConversationId])REFERENCES [dbo].[Conversation] ([ConversationId])GOALTER TABLE [dbo].[Messages] CHECK CONSTRAINT [FK_Messages_Conversation]ALTER TABLE [dbo].[Messages] CHECK CONSTRAINT [FK_Messages_Conversation]GO 这篇关于事务性插入期间实体框架忽略了引用Contsraint的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-30 05:53