实体框架,区分列,但没有继承 [英] Entity framework, discriminator column, but no inheritance

查看:48
本文介绍了实体框架,区分列,但没有继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测试一个非常简单的Code-First示例。我有一个叫做 PurchaseItem 的类,它不是从任何基类继承的,也没有其他类可以继承。它与其他模型完全没有关联:

I want to test a very simple Code-First example. I have a class called PurchaseItem which is not inherited from any based class and also no other class inherits from it. It has no association with other models at all:

public class PurchaseItem
{
    public int Id { get; set; } 
    public string Buyer { get; set; }
    public string Item { get; set; }
    public int Quantity { get; set; }
    public int Price { get; set; }
}

这是我的数据库上下文代码。我将数据库初始化程序设置为 null ,因为我已经有数据库:

Here's my database context code. I set the database initializer to null, because I already have database:

public class MiniContext : DbContext
{
    public MiniContext()
    {
        Database.SetInitializer<MiniContext>(null);
    }

    public DbSet<PurchaseItem> PurchaseItems { get; set; }
}

这是保存记录的代码:

public void SavePurchaseItems(string buyer, string item, int quantity, int price)
{
    PurchaseItem purchaseItem = new PurchaseItem
    {
        Buyer = buyer,
        Item = item,
        Quantity = quantity,
        Price = price
    };
    using (MiniContext context = new MiniContext())
    {
        context.PurchaseItems.Add(purchaseItem);
        context.SaveChanges();
    }
}

没有继承。完全没有继承。只是一个简单的独立表。但是我得到的是:

No inheritance. No inheritance at all. Just a simple, stand-alone table. But what I get is:


无效的列名'Discriminator'。

Invalid column name 'Discriminator'.

context.SaveChanges(); 行上。怎么了?我该怎么办?

On context.SaveChanges(); line. What's wrong? What should I do?

更新:我已经看到 EF代码第一个无效的列名'Discriminator',但没有继承问题 。这个问题没有解决我的问题。在这个问题上,OP确实在其代码中具有继承性。

Update: I've already seen the EF Code First "Invalid column name 'Discriminator'" but no inheritance question. That question hasn't solved my problem. In that question, the OP do has inheritance in his code.

推荐答案

您的连接字符串可能与您的上下文不同!
请检查此文章

Your connection string might differ from your context! Please check this article.

实体框架遵循> 基于配置的公约 思想流派。这意味着即使在找到相关的连接字符串时,Entity Framework也会使用上下文类的名称(在本例中为 MiniAccounting ),并尝试查找具有该名称的连接字符串。在配置文件中。

Entity Framework follows the Convention over Configuration school of thought. This means that even in finding the related connection string, Entity Framework, uses the name of your context class (in this case MiniAccounting) and tries to find a connection string with that name in the configuration file.

但是,如果找不到任何内容,则可以使用SQL的精简版本(我猜是SQL CE或SQL Express,不确定

However, if it doesn't find anything, it's fallback to light version of SQL (I guess SQL CE, or SQL Express, not sure about it), creates a database by itself, and tries to insert into it.

在您的情况下,请检查配置文件,并查看它是否与您的上下文名称匹配类。如果不是,请使用此构造函数:

In your case, check the configuration file, and see if it matches the name of your context class. If not, user this constructor:

public class MiniContext : DbContext
{
    public MiniContext()
         : base("YourConnectionStringNameHere")
    {
        Database.SetInitializer<MiniContext>(null);
    }

    public DbSet<PurchaseItem> PurchaseItems { get; set; }
}

这篇关于实体框架,区分列,但没有继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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