EF代码第一个模式生成问题

时间:2011-05-13 10:39:19

标签: entity-framework-4.1 code-first

为什么这个映射(在SQL CE 4.0上)

ToTable("USERS");
      HasKey(c => c.Id);
      Property(c => c.Id).HasColumnName("USERS_ID");
      Property(c => c.ActiveDirectoryUsername).HasColumnName("AD_ID");
      Property(c => c.LastUpdated).HasColumnName("LastUpdated").IsOptional();
      Property(c => c.Active).HasColumnName("Active").IsOptional();
      Property(c => c.AccountType).HasColumnName("AccountType").IsOptional();
      HasOptional(c => c.Contact).WithMany().Map(c => c.MapKey("CONTACT_ID"));
      Map<UserCompanyLink>(m => m.Requires("IsCompanyDelegate").HasValue(1));
      Map<UserDirectorLink>(m => m.Requires("IsCompanyDelegate").HasValue(0));

生成此错误

SetUp : System.InvalidOperationException : The database creation succeeded, but the creation of the database objects did not. See inner exception for more details.
  ----> System.Data.SqlServerCe.SqlCeException : A column ID occurred more than once in the specification.

我希望能够看到正在生成的架构sql来调试这个......

修改

将IsCompanyDelegate添加到UserLink基础实体后,我现在得到

SetUp : System.Data.DataException : An exception occurred while initializing the database. See the InnerException for details.
  ----> System.Data.EntityCommandCompilationException : An error occurred while preparing the command definition. See the inner exception for details.
  ----> System.Data.MappingException : 
(69,10) : error 3023: Problem in mapping fragments starting at lines 69, 79, 88, 172:Column UserLink.IsCompanyDelegate has no default value and is not nullable. A column value is required to store entity data.
An Entity with Key (PK) will not round-trip when:
  ((PK is NOT in 'UserLinks' EntitySet OR Entity is type [eServices.Admin.Data.Contexts.UserLink]) AND (PK is in 'UserLinks' EntitySet OR PK plays Role 'UserLink_Contact_Source' in AssociationSet 'UserLink_Contact'))

更加神秘的gobbledygook。

为什么映射TPH太复杂了。代码的重点首先是能够根据需要创建模型吗?

修改

所以看起来没有数据库存在,我们得到第一个错误(即第一次测试运行),所以第二个错误可能是无关紧要的。

1 个答案:

答案 0 :(得分:0)

原来这是一个与这种特殊关系无关的随机错误。

我将连接从SQL Server CE更改为SQL Server 2008,并且我收到了一条更有用的错误消息。这次它告诉我哪个列ID和哪个表。

故事的道德,如果你使用SQL Server CE,试试它的大哥,你可能会有更多的运气。