【发布时间】:2012-05-03 19:38:36
【问题描述】:
我正在尝试使用带有数据注释的 Code First 和 Entity Framework 4.3 创建一个计费数据库,每次尝试创建我的数据库时都会出错。以下是我正在处理的对象:
public class ClientBase
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ClientID { get; set; }
[Required]
public string ClientName { get; set; }
[Required]
public bool IsActive { get; set; }
[Required]
public string ClientContactName { get; set; }
[Required]
public string ClientContactEmail { get; set; }
public virtual List<PropertyBase> Communities { get; set; }
}
public class PropertyBase
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int PropertyID { get; set; }
[ForeignKey("Client")]
public int ClientID { get; set; }
[Required, EnumDataType(typeof(BillingFrequency))]
public BillingFrequency PropertyBillingFrequency { get; set; }
[Required]
public bool IsActive { get; set; }
[Required]
public string PropertyName { get; set; }
public string PropertyStreet { get; set; }
public string PropertyCity { get; set; }
public string PropertyState { get; set; }
public int PropertyZipCode { get; set; }
public virtual ClientBase Client { get; set; }
}
无论我尝试做什么,我总是得到这个错误:
The operation failed because an index or statistics with name 'IX_ClientID' already exists on table 'Property'.
我已经使用 Fluent API 看到了这个问题的解决方案,但我还没有找到用于数据注释的解决方案。
有人知道如何解决这个问题吗?
编辑:这是 DbContext 中的代码:
public DbSet<ClientBase> ClientBases { get; set; }
public DbSet<PropertyBase> PropertyBases { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
base.OnModelCreating(modelBuilder);
}
【问题讨论】:
-
这一切都适合我。我不明白 table 'Property' 在这个配置中来自哪里。
-
哎呀!我忘了把 [Table("Property")] 放在 PropertyBase 类上。
-
在创建索引的 SQL 服务器上是否有运行触发器?我不知道它(甚至是第一个)会来自哪里。
-
我猜你做到了,但你是否尝试过确保你在 Db 中没有任何东西开始 - 并确保你知道它“去向”(你的连接配置是什么,defaultConnectionFactory 等) ,你在哪里得到错误?使用迁移,或尝试使用它?你如何调用上下文,初始化等。尝试创建一个新项目,新连接等,即从头开始......
标签: c# entity-framework data-annotations entity-framework-4.3