C# 如何修复';实体类型。。。需要定义主键';?
我正在做一个基于C#和Visual Studio代码的internet脚本模块。虽然它是以一种非常循序渐进的方式教授的,但我想,在模块开始时,我确实很挣扎,因为我不熟悉VSC,而且有时会变得非常混乱和慌乱。不管怎样,我现在进步了不少,讲师说他认为基于我自己的数据库编写我自己的VSC项目对我来说是有益的。这对我来说绝对很有吸引力,因为我已经有了一个数据库供我自己使用,我想与之合作,这将有助于我的动机和兴趣 因此,昨天我花了一整天的时间再一次跟踪整个过程,但这次使用的是我自己的数据库。我做了很多工作,项目构建得很好,但显然有些地方出了问题,因为当我尝试运行它时,我发现 未处理的异常。System.InvalidOperationException:实体类型“Item”需要定义主键 我不会粘贴整个错误消息,因为它是大量的,但它也引用了“Program.cs:Line19” 这是我的密码: 这是我的密码: DHR.csprojC# 如何修复';实体类型。。。需要定义主键';?,c#,visual-studio-code,entity-framework-core,C#,Visual Studio Code,Entity Framework Core,我正在做一个基于C#和Visual Studio代码的internet脚本模块。虽然它是以一种非常循序渐进的方式教授的,但我想,在模块开始时,我确实很挣扎,因为我不熟悉VSC,而且有时会变得非常混乱和慌乱。不管怎样,我现在进步了不少,讲师说他认为基于我自己的数据库编写我自己的VSC项目对我来说是有益的。这对我来说绝对很有吸引力,因为我已经有了一个数据库供我自己使用,我想与之合作,这将有助于我的动机和兴趣 因此,昨天我花了一整天的时间再一次跟踪整个过程,但这次使用的是我自己的数据库。我做了很多工作
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.0.0" />
</ItemGroup>
</Project>
编辑:忘了提了,我试过
[Key]
public int listing_id { get; set; }
…但我得到了
找不到类型或命名空间名称“KeyAttribute”(是否缺少using指令或程序集引用?)
您的类
项
没有定义主键-解决方案是:定义主键
EF Core有一个约定,即名称为Id
或(实体)Id
(此处:ItemId
)的数字列将自动识别为主键
如果您没有这两列中的任何一列,那么您需要手动并通过添加[key]
数据注释明确定义一列(或列的组合)作为主键
因此,可以将int-Id
或int-ItemId
列添加到Item
类中,或者将listing_-Id
列指定为主键,如下所示:
// use the DataAnnotation package - you might need to add it using NuGet
using System.ComponentModel.DataAnnotations;
[Table("Items")]
public class Item //: DbContext
{
[Key]
public int listing_id { get; set; }
...
}
listing\u id
没有使用EF手动识别的命名约定。您必须添加对System.ComponentModel.DataAnnotations的引用,Ctrl+。
在这些情况下确实可以帮助您。@Crowcoder-我已经尝试过了,请查看对我的操作的编辑。谢谢。编辑-好,我添加了键和对System.ComponentModel.DataAnnotations的引用。这似乎有效,但我现在得到的是“没有这样的表:项目”。不过我似乎更进一步了,所以谢谢你。我看到了你的编辑,但是我没有看到任何关于添加对[key]
所在程序集的引用的内容。也许你错过了我的编辑,但我已经尝试过了,我得到了“找不到类型或命名空间名称'KeyAttribute'(是否缺少using指令或程序集引用?)。顺便说一句,讲师的演示视频中没有显示这一点,在演示视频中,他所做的事情与我试图对数据库所做的事情完全相同。如果您按照我的操作中的链接进行操作,您可以自己查看。尽管如此,谢谢。@double happiness:您需要确保在文件的开头有一个使用System.ComponentModel.DataAnnotations;的。-t他KeyAttribute
是在数据注释库中定义的,现在知道了,谢谢。不过我现在得到的是“没有这样的表:项”。那么-在您的数据库中,该表叫什么?您可能需要添加另一个数据注释[表(“此处的表名”)]
到您的类……它被称为Items。它显示在OP.Edit中的.sql文件中-我是否只需要DHR.cs中的:DbContext
?
// using Microsoft.EntityFrameworkCore;
namespace DHR
{
public class Item //: DbContext
{
public int listing_id { get; set; }
public string artist { get; set; }
public string title { get; set; }
public string label { get; set; }
public string catno { get; set; }
public string format { get; set; }
public int release_id { get; set; }
public decimal price { get; set; }
public string listed { get; set; }
public string comments { get; set; }
public string media_condition { get; set; }
public string sleeve_condition { get; set; }
public int weight { get; set; }
public int format_quantity { get; set; }
public string location { get; set; }
public string basic_format { get; set; }
public char label_type { get; set; }
public char sleeve_type { get; set; }
public char with_cover { get; set; }
public char complete { get; set; }
public string country { get; set; }
public int year { get; set; }
public decimal median_price { get; set; }
public string style { get; set; }
public string amaz { get; set; }
public string feed_product_type { get; set; }
public string binding { get; set; }
public string vinyl_record_details { get; set; }
public string condition_type { get; set; }
public string shipping_group { get; set; }
public string product_id { get; set; }
public string product_id_type { get; set; }
public bool image { get; set; }
public string image_url { get; set; }
// public DbSet<Item> Items { get; set; }
// protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
// {
// // string path = System.IO.Path.Combine(System.Environment.CurrentDirectory, "DHR.db");
// string CurrentDir = System.Environment.CurrentDirectory;
// string ParentDir = System.IO.Directory.GetParent(CurrentDir).FullName;
// string path = System.IO.Path.Combine(ParentDir, "DHR.db");
// optionsBuilder.UseSqlite($"Filename={path}");
// }
}
}
--
-- File generated with SQLiteStudio v3.2.1 on Fri Mar 19 15:39:12 2021
--
-- Text encoding used: System
--
PRAGMA foreign_keys = off;
BEGIN TRANSACTION;
-- Table: Items
CREATE TABLE Items (listing_id INTEGER PRIMARY KEY NOT NULL, artist STRING, title STRING, label STRING, catno STRING, format STRING, release_id INTEGER, price DECIMAL, listed STRING, comments STRING, media_condition STRING, sleeve_condition STRING, weight INTEGER, format_quantity INTEGER, location STRING, basic_format STRING, label_type CHAR (2), sleeve_type CHAR (2), with_cover CHAR (2), complete CHAR (1), country STRING, year INTEGER, median_price DECIMAL, style STRING, amaz STRING, feed_product_type STRING, binding STRING, vinyl_record_details STRING, condition_type STRING, shipping_group STRING, product_id STRING, product_id_type STRING, image BOOLEAN, image_url STRING);
[etc.]
[Key]
public int listing_id { get; set; }
// use the DataAnnotation package - you might need to add it using NuGet
using System.ComponentModel.DataAnnotations;
[Table("Items")]
public class Item //: DbContext
{
[Key]
public int listing_id { get; set; }
...
}