简体   繁体   English

MySQL /实体框架-生成模型时发生错误“ RelationshipDetails中列“ FkColumn为DBNull”的值”

[英]MySQL/Entity Framework - Error 'The value for column 'FkColumn in RelationshipDetails is DBNull' occurs when generating the model

I am trying to use MySql 5.1 with Entity Framework 6.0 and the DbContext generator 5.0, with .Net Framework 4.0, Visual Studio 2010 and C#. 我正在尝试将MySql 5.1与Entity Framework 6.0和DbContext生成器5.0,.Net Framework 4.0,Visual Studio 2010和C#结合使用。 I have downloaded the most recent packages from NuGet. 我已经从NuGet下载了最新的软件包。 I am able to add tables from the database to the model with no problems up to a point (which I have had difficulties pinning down). 我能够将数据库中的表添加到模型中,一点都没有问题(我很难确定)。 After that point, trying to add new tables or even generating the existing model gives the error shown below: 之后,尝试添加新表甚至生成现有模型都会显示以下错误:

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'.

I have tried turning on debug in the .tt file, but the exception occurs before the .tt file is loaded and being parsed. 我尝试在.tt文件中打开调试,但是在加载.tt文件并对其进行解析之前会发生异常。 I have also tried deleting tables, but the error continues to occur when I try and upload new tables from the database, or try to save the existing file. 我也尝试删除表,但是当我尝试从数据库中上载新表或尝试保存现有文件时,该错误继续发生。 Oddly enough, the tt template does get called, and generats the classes. 奇怪的是,确实调用了tt模板,并生成了类。 But it will not load any new tables from the database. 但是它不会从数据库中加载任何新表。 When you run the debugger on the template, you get the following message: 在模板上运行调试器时,会出现以下消息:

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   

Does any one have any suggestions on how to proceed on this? 有人对如何进行此操作有任何建议吗? I am at a standstill and have deleted and recreated my model 3 times now, without getting any closer to figuring out what is causing this. 我处于停滞状态,现在已经删除并重新创建了3次模型,而没有弄清是什么原因造成的。

Thanks, Neil 谢谢,尼尔

A little bit more research seems to indicate that once an entity named Contact is defined, adding any further entities that link to that entity causes the error. 更多的研究似乎表明,一旦定义了名为Contact的实体,则添加任何其他链接到该实体的实体都会导致错误。 I can add other entities that are not linked to the Contact, without problems. 我可以添加没有链接到联系人的其他实体,而不会出现问题。 The Contact entity is defined as follows: 联系人实体的定义如下:

     <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>

The MySQL table definition is: 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';

I finally deleted the database and recreated it from the MySQL Workbench model and the problem went away. 我终于删除了数据库,并从MySQL Workbench模型中重新创建了数据库,问题消失了。 Clearly, something in MySQL was corrupted from the repeated uploads from the Model. 显然,由于从模型中重复上传,MySQL中的某些内容已损坏。 However, recreating from the model created a different problem, described here . 然而,从模型重建创造了一个不同的问题,说明这里 The fun never stops when you work with OpenSource! 当您使用OpenSource时,乐趣永远不会停止!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM