CTP5 Code First“实体不支持错误” [英] CTP5 Code First "entity not supported error"

查看:59
本文介绍了CTP5 Code First“实体不支持错误”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照 http://blogs.msdn.com/b/adonet/archive/2010/12/14/ef-feature-ctp5-code-first-walkthrough.aspx  但是我得到了
初始化ProductContext的InvalidOperationException,其中包含消息"类型'ModelFirstSample.Program + Product'不是受支持的实体类型。"

I followed the example from http://blogs.msdn.com/b/adonet/archive/2010/12/14/ef-feature-ctp5-code-first-walkthrough.aspx however I am getting the a InvalidOperationException on initializing the ProductContext with the message "The type 'ModelFirstSample.Program+Product' is not a supported entity type."

这是我的代码(其中就我所知,在示例中是相同的。

Here is my code (which is the same as in the example as far as i know).


using System;
using System.Collections.Generic;
using System.Data.Entity;

namespace ModelFirstSample
{
  class Program
  {
    static void Main(string[] args)
    {
      using (var db = new ProductContext())
      {
        // Add a food category 
        var food = new Category { Id = "FOOD", Name = "Foods" };
        db.Categories.Add(food);
        int recordsAffected = db.SaveChanges();

        Console.WriteLine(
          "Saved {0} entities to the database, press any key to exit.",
          recordsAffected);

        Console.ReadKey();
      }
    }

    public class Category
    {
      public string Id { get; set; }
      public string Name { get; set; }

      public virtual ICollection<Product> Products { get; set; }
    }

    public class Product
    {
      public int Id { get; set; }
      public string Name { get; set; }
      public string CategoryId { get; set; }

      public virtual Category Category { get; set; }
    }

    public class ProductContext : DbContext
    {
      public DbSet<Product> Products { get; set; }
      public DbSet<Category> Categories { get; set; }
    }
  }
}

推荐答案

 问题是类别和产品是否嵌套在Program类中(Code First不支持嵌套类)。

The issue is that Category and Product are nested inside the Program class (Code First does not support nested classes).

如果将结束Program类的花括号移动到Category类定义之上,那么一切都应该有效。

If you move the curly brace that ends the Program class to above the Category class definition then everything should work.

~Rowan


这篇关于CTP5 Code First“实体不支持错误”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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