混合模型优先和代码优先

发布于 2024-11-27 10:51:17 字数 1010 浏览 0 评论 0 原文

我们使用模型优先方法创建了一个 Web 应用程序。一名新开发人员进入该项目,并使用代码优先方法(使用数据库文件)创建了一个新的自定义模型。这里

是代码优先的数据库上下文。

namespace WVITDB.DAL
{
public class DashboardContext : DbContext
{
    public DbSet<CTOReview> CTOReviews { get; set; }
    public DbSet<Concept> Concepts { get; set; }

    //public DashboardContext()
    //    : base("name=DashboardContext")
    //{

    //}


  //  protected override void OnModelCreating(DbModelBuilder modelBuilder)
   // {
   //     //modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
   // }
}
}

以下控制器方法抛出错误无法找到“WVITDB.Models.FavoriteProject”的概念模型类型。并引用原始数据库模型。我们不确定它为什么(或如何)这么称呼。

  public ViewResult Index()
        {
            var d = db.Concepts.ToList(); //Throws error here
            return View("Index",d);
        }

实例化 DashboardContextclass 时,两个 DBset 属性都会显示错误。

控制器调用错误的数据库是否有原因?

编辑:

FavoriteProject 处于不同的上下文(我们的主要数据模型)中,与新的自定义模型无关。

We created a web application using the model first approach. A new developer came into the project and created a new custom model using the code first approach (using a database file). The

Here is the code first database context.

namespace WVITDB.DAL
{
public class DashboardContext : DbContext
{
    public DbSet<CTOReview> CTOReviews { get; set; }
    public DbSet<Concept> Concepts { get; set; }

    //public DashboardContext()
    //    : base("name=DashboardContext")
    //{

    //}


  //  protected override void OnModelCreating(DbModelBuilder modelBuilder)
   // {
   //     //modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
   // }
}
}

The following controller method throws an error Could not find the conceptual model type for 'WVITDB.Models.FavoriteProject'. and refers to the original database model. We are not sure why (or how) it is calling that.

  public ViewResult Index()
        {
            var d = db.Concepts.ToList(); //Throws error here
            return View("Index",d);
        }

When the DashboardContextclass is instantiated the error are shows up for both of the DBset properties.

Is there are a reason why the controller calling the wrong database?

EDIT:

FavoriteProject is in a different context (our main data model) and not related to the new custom model.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云归处 2024-12-04 10:51:17

找到了答案,但这可能不是您想听到的:

http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d2a07542-cb33-48ba-86ed-4fdc70fb3f1a

“如果您使用默认代码生成对于 EDMX 文件,生成的类包含一系列属性,以帮助 EF 找到要用于每个实体类型的类,EF 当前有一个限制。 POCO 类无法从包含具有 EF 属性的类的程序集中加载(简短的答案是否定的,您的类需要位于单独的项目中),

这是一个有点人为的限制,我们意识到这是痛苦的,并将尝试。并在将来删除。”

因此,解决方法是将类拆分为两个不同的程序集。

Found an answer, it maybe not what you want to hear though:

http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d2a07542-cb33-48ba-86ed-4fdc70fb3f1a

"If you are using the default code generation for the EDMX file then the generated classes contain a series of attributes to help EF find which class to use for each entity type. EF currently has a restriction that POCO classes can't be loaded from an assembly that contains classes with the EF attributes. (Short answer is no, your classes need to be in separate projects).

This is a somewhat artificial restriction and something that we realize is painful and will try and remove in the future."

So the workaround would be to split the classes into two different assemblies.

想念有你 2024-12-04 10:51:17

ajpaz - 您可能必须以编程方式将现有模型“映射”到数据库表。我从错误消息中假设它正在寻找 FavouriteProject 表/类映射。也许数据库将其定义为单数,在这种情况下尝试:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<FavouriteProjects>().ToTable("FavouriteProject");
}

您还需要按照上面的方式做一个数据库集(或复数的一些排列):

public DbSet<FavouriteProjects> FavouriteProjects{ get; set; }

可能很遥远,只是一个想法

[编辑]< /strong> - 覆盖 web.config 中的代码第一个 dbcontext(在连接字符串下 [名称 必须 与您的 dbcontext 名称匹配]):

<add name="DashboardContext" connectionString="Data Source=remote.sqlserver.server;Initial Catalog=code_first_db;Persist Security Info=True;MultipleActiveResultSets=True;User ID=code_first_db_user;Password=password" providerName="System.Data.SqlClient"/>

ajpaz - you may have to 'map' your existing model to the database table programatically. i'm assuming from the error message that its looking for the FavouriteProject table/class mapping. maybe the db has this defined as a singular, in which case try:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<FavouriteProjects>().ToTable("FavouriteProject");
}

you'd also need to do a dbset as per above (or some permutation of plurals):

public DbSet<FavouriteProjects> FavouriteProjects{ get; set; }

might be miles off, just a thought

[edit] - overiding your code 1st dbcontext in web.config (under connectionstrings [name MUST match your dbcontext name]):

<add name="DashboardContext" connectionString="Data Source=remote.sqlserver.server;Initial Catalog=code_first_db;Persist Security Info=True;MultipleActiveResultSets=True;User ID=code_first_db_user;Password=password" providerName="System.Data.SqlClient"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文