本文介绍了EF 4.1 OnModelCreating不叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有EF 4.1没有叫 OnModelCreating 的一个问题,这样我可以配置表等。我有一个现有的数据库。这是我的连接字符串:

I have a problem with EF 4.1 not calling OnModelCreating so that I can configure tables etc. I have an existing database. Here is my connection string:

<add name="AcmeDBContext"
     connectionString="metadata=res://*/|res://*/|res://*/;
                       provider=System.Data.SqlClient;
                       provider connection string=&quot;
                       data source=[server];
                       initial catalog=Acme;integrated security=True;
                       multipleactiveresultsets=True;App=EntityFramework&quot;"
     providerName="System.Data.EntityClient" />

下面是我从类继承的DbContext

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

  protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
    modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    modelBuilder.Entity<Vehicle>().ToTable("tblVehicle");
    modelBuilder.Entity<VehicleMake>().ToTable("tblVehicleMake");
    modelBuilder.Entity<VehicleModel>().ToTable("tblVehicleModel");
    base.OnModelCreating(modelBuilder);
  }

 }

我已阅读并在线阅读,但我不能指出是什么问题。在 OnModelCreating 不会被调用,因此例外说,实体/类(车辆,VehicleMake,VehicleModel)不要在目前的情况下存在,当我试图查询任何东西。

I have read and read online, but I cannot point out what the problem is. The OnModelCreating is never called and hence the exceptions saying that the entity/classes (Vehicle, VehicleMake, VehicleModel) do not exist in the current context when I try to query anything.

推荐答案

您正在使用两种方法一起 - 在连接字符串说你要使用模型的第一或数据库第一与EDMX,但你正在写上下文中使用code首先/流利的API映射数据库。一旦你开始使用EDMX OnModelCreating 永远不会被调用。如果你想使用使用常见的SQL连接字符串没有元数据资源 OnModelCreating

You are using two approaches together - your connection string says that you want to use model first or database first with EDMX but you are writing context to use code first / fluent api to map database. Once you use EDMX OnModelCreating is never called. Use common SQL connection string without metadata resources if you want to use OnModelCreating.

<add name="AcmeDBContext"
     connectionString="data source=[server];
                       initial catalog=Acme;integrated security=True;
                       multipleactiveresultsets=True;App=EntityFramework"
     providerName="System.Data.SqlClient" />

这篇关于EF 4.1 OnModelCreating不叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-25 08:25