Asp.net核心实体框架找不到IndexAttribute

编程入门 行业动态 更新时间:2024-10-27 00:34:13
本文介绍了Asp核心实体框架找不到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

本文发布于:2023-11-08 22:11:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1570591.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:找不到   实体   框架   核心   Asp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!