C# MySQL/实体框架-错误';列'的值;RelationshipDetails中的fk列为DBNull';在生成模型时发生
我正在尝试将MySql 5.1与Entity Framework 6.0和DbContext generator 5.0、.Net Framework 4.0、Visual Studio 2010和C#一起使用。我已经从NuGet下载了最新的软件包。我能够将表从数据库添加到模型中,在某种程度上没有问题(我很难确定)。在该点之后,尝试添加新表或甚至生成现有模型会出现如下错误:C# MySQL/实体框架-错误';列'的值;RelationshipDetails中的fk列为DBNull';在生成模型时发生,c#,mysql,visual-studio-2010,entity-framework,C#,Mysql,Visual Studio 2010,Entity Framework,我正在尝试将MySql 5.1与Entity Framework 6.0和DbContext generator 5.0、.Net Framework 4.0、Visual Studio 2010和C#一起使用。我已经从NuGet下载了最新的软件包。我能够将表从数据库添加到模型中,在某种程度上没有问题(我很难确定)。在该点之后,尝试添加新表或甚至生成现有模型会出现如下错误: Unable to generate the model because of the following excepti
Unable to generate the model because of the following exception: 'The value for column 'FkColumn' in table 'RelationshipDetails' is DBNull.
Unable to cast object of type 'System.DBNull' to type 'System.String'.
我已尝试在.tt文件中启用调试,但在加载和解析.tt文件之前会发生异常。我也尝试过删除表,但当我尝试从数据库上载新表或尝试保存现有文件时,错误仍然会发生。奇怪的是,tt模板确实会被调用,并生成类。但它不会从数据库中加载任何新表。在模板上运行调试器时,会收到以下消息:
Error 2 An Exception was thrown while running the transformation code. The process cannot continue. The following Exception was thrown:
System.Runtime.Remoting.RemotingException: Object '/9747a151_8703_4922_90da_93472afe62d2/fjgxduxp4yb2qwls+ipl7t98_89.rem' has been disconnected or does not exist at the server.
at Microsoft.VisualStudio.TextTemplating.TransformationRunner.get_Errors()
at Microsoft.VisualStudio.TextTemplating.Engine.CompileAndRunCode(String generatorCode, ITextTemplatingEngineHost host, TemplateProcessingSession session) C:\Development\CCBPlus\CCB Server\Model\CCBPlus.tt 0 0
有人对如何进行这项工作有什么建议吗?我现在处于停顿状态,已经删除并重新创建了3次我的模型,但还没有进一步弄清楚是什么原因造成了这种情况
谢谢,尼尔
更多的研究似乎表明,一旦定义了名为Contact的实体,添加链接到该实体的任何其他实体都会导致错误。我可以添加其他未链接到联系人的实体,不会出现问题。联系人实体的定义如下:
<EntityType Name="Contact">
<Key>
<PropertyRef Name="ContactId" />
</Key>
<Property Type="Int32" Name="ContactId" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
<Property Type="Byte" Name="ContactType" Nullable="false" />
<Property Type="String" Name="DisplayName" Nullable="false" MaxLength="80" FixedLength="false" Unicode="false" />
<Property Type="String" Name="SearchName" Nullable="false" MaxLength="80" FixedLength="false" Unicode="false" />
<Property Type="String" Name="Prefix" MaxLength="15" FixedLength="false" Unicode="false" />
<Property Type="String" Name="LastName" MaxLength="50" FixedLength="false" Unicode="false" />
<Property Type="String" Name="Initials" MaxLength="10" FixedLength="false" Unicode="false" />
<Property Type="String" Name="FirstName" MaxLength="40" FixedLength="false" Unicode="false" />
<Property Type="String" Name="Suffix" MaxLength="10" FixedLength="false" Unicode="false" />
<Property Type="String" Name="OrganizationName" MaxLength="50" FixedLength="false" Unicode="false" />
<Property Type="String" Name="InCareOf" MaxLength="80" FixedLength="false" Unicode="false" />
<Property Type="DateTime" Name="Birthdate" />
<Property Type="DateTime" Name="Deceased" />
<Property Type="String" Name="SocialSecurityNumber" MaxLength="20" FixedLength="false" Unicode="false" />
<Property Type="SByte" Name="Gender" />
<Property Type="String" Name="CulturePreference" MaxLength="10" FixedLength="false" Unicode="false" />
<Property Type="DateTime" Name="LastMeeting" />
<Property Type="String" Name="Hobbies" MaxLength="100" FixedLength="false" Unicode="false" />
<Property Type="String" Name="Notes" MaxLength="1073741823" FixedLength="false" Unicode="false" />
<Property Type="Boolean" Name="Inactive" Nullable="false" />
<Property Type="DateTime" Name="Created" Nullable="false" />
<Property Type="DateTime" Name="LastModified" Nullable="false" />
<NavigationProperty Name="AccountHolders" Relationship="CCB.EntityModel.fk_AccountHolder_Contact" FromRole="Contact" ToRole="AccountHolder" />
<NavigationProperty Name="Branches" Relationship="CCB.EntityModel.fk_Branch_ContactInfo" FromRole="Contact" ToRole="Branch" />
<NavigationProperty Name="Company" Relationship="CCB.EntityModel.fk_Company_ContactInfo" FromRole="Contact" ToRole="Company" />
<NavigationProperty Name="ContactCategories" Relationship="CCB.EntityModel.fk_ContactCategory_Contact" FromRole="Contact" ToRole="ContactCategory" />
<NavigationProperty Name="ContactCommunications" Relationship="CCB.EntityModel.fk_ContactCommunication_Contact" FromRole="Contact" ToRole="ContactCommunication" />
<NavigationProperty Name="Agents" Relationship="CCB.EntityModel.fk_Agent_ContactInfo" FromRole="Contact" ToRole="Agent" />
</EntityType>
<Association Name="fk_AccountHolder_Contact">
<End Type="CCB.EntityModel.Contact" Role="Contact" Multiplicity="1" />
<End Type="CCB.EntityModel.AccountHolder" Role="AccountHolder" Multiplicity="*" />
<ReferentialConstraint>
<Principal Role="Contact">
<PropertyRef Name="ContactId" />
</Principal>
<Dependent Role="AccountHolder">
<PropertyRef Name="ContactId" />
</Dependent>
</ReferentialConstraint>
</Association>
<Association Name="fk_Branch_ContactInfo">
<End Type="CCB.EntityModel.Contact" Role="Contact" Multiplicity="1" />
<End Type="CCB.EntityModel.Branch" Role="Branch" Multiplicity="*" />
</Association>
<Association Name="fk_Company_ContactInfo">
<End Type="CCB.EntityModel.Contact" Role="Contact" Multiplicity="1" />
<End Type="CCB.EntityModel.Company" Role="Company" Multiplicity="0..1" />
<ReferentialConstraint>
<Principal Role="Contact">
<PropertyRef Name="ContactId" />
</Principal>
<Dependent Role="Company">
<PropertyRef Name="CompanyContactId" />
</Dependent>
</ReferentialConstraint>
</Association>
<Association Name="fk_ContactCategory_Contact">
<End Type="CCB.EntityModel.Contact" Role="Contact" Multiplicity="1" />
<End Type="CCB.EntityModel.ContactCategory" Role="ContactCategory" Multiplicity="*" />
<ReferentialConstraint>
<Principal Role="Contact">
<PropertyRef Name="ContactId" />
</Principal>
<Dependent Role="ContactCategory">
<PropertyRef Name="ContactId" />
</Dependent>
</ReferentialConstraint>
</Association>
<Association Name="fk_ContactCommunication_Contact">
<End Type="CCB.EntityModel.Contact" Role="Contact" Multiplicity="1" />
<End Type="CCB.EntityModel.ContactCommunication" Role="ContactCommunication" Multiplicity="*" />
</Association>
我最终删除了数据库并从MySQL工作台模型中重新创建了它,问题就消失了。很明显,MySQL中的某些内容由于模型的重复上传而损坏。但是,根据模型重新创建会产生另一个问题,如所述。当你使用开源时,乐趣永远不会停止
CREATE TABLE `Contacts` (
`ContactId` int(11) NOT NULL,
`ContactType` bit(3) NOT NULL COMMENT 'Contact Type: 0=Individual, 1=Corporate, 2=Family, 3=Branch, 4=Department, 5=Group, 6=Fund/GIC/Bank company',
`DisplayName` varchar(80) NOT NULL,
`SearchName` varchar(80) NOT NULL,
`Prefix` varchar(15) DEFAULT NULL,
`LastName` varchar(50) DEFAULT NULL,
`Initials` varchar(10) DEFAULT NULL,
`FirstName` varchar(40) DEFAULT NULL,
`Suffix` varchar(10) DEFAULT NULL,
`OrganizationName` varchar(50) DEFAULT NULL,
`InCareOf` varchar(80) DEFAULT NULL COMMENT 'If mail is to be sent with the line "In care of [X]"',
`Birthdate` datetime DEFAULT NULL,
`Deceased` datetime DEFAULT NULL,
`SocialSecurityNumber` varchar(20) DEFAULT NULL,
`Gender` bit(1) DEFAULT b'0' COMMENT '0=Male,1=Female',
`CulturePreference` varchar(10) DEFAULT 'EN-CA' COMMENT 'Global Culture variable, e.g. EN-CA or FR-CA or other.',
`LastMeeting` datetime DEFAULT NULL,
`Hobbies` varchar(100) DEFAULT NULL,
`Inactive` tinyint(1) NOT NULL DEFAULT '0',
`Created` datetime NOT NULL,
`LastModified` datetime NOT NULL,
PRIMARY KEY (`ContactId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Stores contact and optionally personal information';