Asp.net核心实体框架找不到IndexAttribute [英] Asp.net Core Entity Framework cannot find IndexAttribute

查看:106
本文介绍了Asp.net核心实体框架找不到IndexAttribute的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

执行"dotnet run"后,在IDE和控制台窗口中都从Mac上的Visual Studio Code中收到以下消息:

找不到类型或名称空间名称'IndexAttribute'

我有一个名为Story的类,我想将其用于使用Code First生成数据库.此类具有一个标有KeyAttribute的主键和一个标有MaxLengthAttribute的Author字符串,因此两者都可以工作(使用System.ComponentModel.DataAnnotations).另外两个字段DateTime Date和bool IsPublished已应用IndexAttribute(这是一个两列索引).我将其明确命名为IX_DatePublished,IsUnique = false,并在日期"字段中使用Order = 1,在"IsPublished"字段中使用Order = 2.

  • 在运行"dotnet restore"之前,我应在project.json中放入什么内容,以使其插入正确的内容以使IndexAttribute正常工作?
  • ASPCore1 for Mac/Linux附带的EF是否没有包含正确的名称空间?

谢谢!

解决方案

自您提出问题以来,这种情况似乎已经改变. jsakamoto 已实现了NuGet程序包,该程序包使您可以保留[Index]属性.唯一的区别是变量的顺序.您不再可以将Order=0作为最后一个变量,而是这样做:

[Index("IX_TreeFiddy", 0, IsUnique = false)]
public string LochNessMonster { get; set; }

[Index("IX_TreeFiddy", 1, IsUnique = false)]
public int CentsGiven { get; set; }

覆盖OnModelCreating()

// Override "OnModelCreating"
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    // .. and invoke "BuildIndexesFromAnnotations"!
    modelBuilder.BuildIndexesFromAnnotations();
}

此处链接到: .NET Core NuGet包的IndexAttribute

I receive the following from Visual Studio Code on my Mac, both in IDE and console window after executing "dotnet run":

The type or namespace name 'IndexAttribute' could not be found

I have a class called Story which I want to use for generating a database with Code First. This class has a primary key marked with KeyAttribute and Author string marked with MaxLengthAttribute, so both of those work (using System.ComponentModel.DataAnnotations). Two more fields, DateTime Date and bool IsPublished, have IndexAttribute applied (it's a two-column index). I explicitly named it IX_DatePublished, IsUnique = false, and use Order = 1 for the Date field and Order = 2 for the IsPublished field.

  • What do I put in project.json before running "dotnet restore" to have it pull in the right stuff for IndexAttribute to work?
  • Does EF included with ASPCore1 for Mac/Linux not have the right namespace included?

Thank you!

解决方案

This seems to have changed since you asked the question. jsakamoto has implemented NuGet package that allows you to keep your [Index] attribute. The only difference is order of variables; you can no longer have Order=0 as your last variable but rather do:

[Index("IX_TreeFiddy", 0, IsUnique = false)]
public string LochNessMonster { get; set; }

[Index("IX_TreeFiddy", 1, IsUnique = false)]
public int CentsGiven { get; set; }

Override OnModelCreating()

// Override "OnModelCreating"
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    // .. and invoke "BuildIndexesFromAnnotations"!
    modelBuilder.BuildIndexesFromAnnotations();
}

Here is link to: IndexAttribute for .NET Core NuGet package

这篇关于Asp.net核心实体框架找不到IndexAttribute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆