[英]C# EF6 index attribute not working
I'm trying to combine two fields into a unique index with EF6. 我正在尝试将两个字段组合成一个带有EF6的唯一索引。 But the update-database command won't add the 'Owner' field into the index.
但update-database命令不会将“Owner”字段添加到索引中。 I'm thinking it's because it's not a primitive type but a FK, but I'm unsure how to fix it.
我在想它是因为它不是原始类型而是FK,但我不确定如何解决它。
public class Fund
{
[Key]
public int FundId { get; set; }
[Required]
[Index("IX_FundNameAndOwner", IsUnique = true, Order = 1)]
[Index("IX_FundIdentifierAndOwner", IsUnique = true, Order=1)]
public ApplicationUser Owner { get; set; }
[Required]
[Index("IX_FundNameAndOwner", IsUnique = true, Order=2)]
[MaxLength(25)]
public string Name { get; set; }
[Required]
[Index("IX_FundIdentifierAndOwner", IsUnique = true, Order=2)]
[MaxLength(25)]
public string Identifier { get; set; }
public double Balance { get; set; }
}
Generated indexes: 生成的索引:
CREATE UNIQUE NONCLUSTERED INDEX [IX_FundIdentifierAndOwner] ON [dbo].[Funds]([Identifier] ASC);
创建独特的非集群索引[IX_FundIdentifierAndOwner] ON [dbo]。[Funds]([Identifier] ASC);
CREATE UNIQUE NONCLUSTERED INDEX [IX_FundNameAndOwner] ON [dbo].[Funds]([Name] ASC);
创建独特的非集群索引[IX_FundNameAndOwner] ON [dbo]。[Funds]([Name] ASC);
CREATE NONCLUSTERED INDEX [IX_Owner_Id] ON [dbo].[Funds]([Owner_Id] ASC);
创建非集群索引[IX_Owner_Id] ON [dbo]。[Funds]([Owner_Id] ASC);
Any help is greatly appreciated! 任何帮助是极大的赞赏!
When you create a navigation property (reference to another table) you should specify the column name to use as foreign key. 创建导航属性(引用另一个表)时,应指定要用作外键的列名。 If you don't do that EF assumes it is a column named Objectname_Id.
如果不这样做,EF假定它是一个名为Objectname_Id的列。
You can see in the sql code that it called your column [Owner_Id]. 您可以在sql代码中看到它调用了您的列[Owner_Id]。 The problem is that when you do it this way you have no control on the data annotations for that column.
问题是,当您这样做时,您无法控制该列的数据注释。
Try this 试试这个
public class Fund
{
[Key]
public int FundId { get; set; }
[Required]
[Index("IX_FundNameAndOwner", IsUnique = true, Order = 1)]
[Index("IX_FundIdentifierAndOwner", IsUnique = true, Order = 1)]
public int OwnerId { get; set; } // <---- ADD THIS!
public virtual ApplicationUser Owner { get; set; }
[Required]
[Index("IX_FundNameAndOwner", IsUnique = true, Order = 2)]
[MaxLength(25)]
public string Name { get; set; }
[Required]
[Index("IX_FundIdentifierAndOwner", IsUnique = true, Order = 2)]
[MaxLength(25)]
public string Identifier { get; set; }
public double Balance { get; set; }
}
Adding a column named ObjectId (or Objec_Id) EF understands by convention that the column is for the property Owner. 添加名为ObjectId(或Objec_Id)的列EF按惯例理解该列是属性所有者。
This is the script generated by the migration: 这是迁移生成的脚本:
CREATE TABLE [dbo].[Funds] (
[FundId] [int] NOT NULL IDENTITY,
[OwnerId] [int] NOT NULL,
[Name] [nvarchar](25) NOT NULL,
[Identifier] [nvarchar](25) NOT NULL,
[Balance] [float] NOT NULL,
CONSTRAINT [PK_dbo.Funds] PRIMARY KEY ([FundId])
)
CREATE UNIQUE INDEX [IX_FundIdentifierAndOwner] ON [dbo].[Funds]([OwnerId], [Identifier])
CREATE UNIQUE INDEX [IX_FundNameAndOwner] ON [dbo].[Funds]([OwnerId], [Name])
Here is an explanation of the default Code First Conventions . 以下是默认Code First Conventions的说明 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.