【问题标题】:MySQL/Entity Framework - Error 'The value for column 'FkColumn in RelationshipDetails is DBNull' occurs when generating the modelMySQL/Entity Framework - 生成模型时出现错误“RelationshipDetails 中列“FkColumn 的值为 DBNull”的值
【发布时间】:2014-03-17 08:06:30
【问题描述】:

我正在尝试将 MySql 5.1 与 Entity Framework 6.0 和 DbContext 生成器 5.0 与 .Net Framework 4.0、Visual Studio 2010 和 C# 一起使用。我已经从 NuGet 下载了最新的包。我能够将数据库中的表添加到模型中,在一定程度上没有问题(我很难确定)。在那之后,尝试添加新表甚至生成现有模型都会出现如下所示的错误:

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 的实体,再添加任何链接到该实体的实体都会导致错误。我可以毫无问题地添加未链接到联系人的其他实体。 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表定义为:

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

【问题讨论】:

    标签: c# mysql visual-studio-2010 entity-framework


    【解决方案1】:

    我终于删除了数据库并从 MySQL Workbench 模型中重新创建它,问题就消失了。显然,MySQL 中的某些内容因模型的重复上传而损坏。但是,从模型重新创建会产生不同的问题,如here 所述。当您使用 OpenSource 时,乐趣永远不会停止!

    【讨论】:

      猜你喜欢
      • 2017-09-23
      • 2018-11-25
      • 1970-01-01
      • 1970-01-01
      • 2017-05-06
      • 1970-01-01
      • 2016-12-28
      • 2014-04-20
      • 1970-01-01
      相关资源
      最近更新 更多