EDMX用于遗留代码_and_代码首先在一个MVC项目中一起进行新的开发 [英] EDMX for legacy code _and_ Code First for new development together in one MVC project

查看:207
本文介绍了EDMX用于遗留代码_and_代码首先在一个MVC项目中一起进行新的开发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况如下:



我们在EF5.0上有一个大型的MVC项目,具有数据库第一个方法:



ObjectContext构造函数:

 命名空间xxx.Models 
{
(...)
public partial class xxxEntities:ObjectContext
{
#region构造函数

///< summary>
///(...)
///< / summary>
public xxxEntities():base(name = xxxEntities,xxxEntities)
{
this.ContextOptions.LazyLoadingEnabled = true;
OnContextCreated();
}

(...)

连接字符串:

 < add name =xxxEntities
connectionString =metadata = res://*/Models.xxxModel .csdl |
res://*/Models.xxxModel.ssdl | res://*/Models.xxxModel.msl;
provider = System.Data.SqlClient; provider connection string =& quot ;
data source =;初始目录= xxxdb;
集成安全= True;
multipleactiveresultsets = True;
App = EntityFramework& quot;
providerName =System.Data.EntityClient/>

我们选择Code First来测试分开的命名空间的新开发。



连接字符串:

 < add name =xxxCFContext
connectionString =数据Source =;
初始目录= xxxdb;
集成安全= True;
用户实例= False;
MultipleActiveResultSets = True
providerName =System.Data。 SqlClient/>

DbContext构造函数:

  namespace xxx.Models.CodeFirst 
{
public partial class xxxCFContext:DbContext
{
static xxxCFContext()
{
数据库.SetInitializer< xxxCFContext>(new ValidateDatabase< xxxCFContext>());
}

public xxxCFContext()
:base(Name = xxxCFContext)
{
}

(。 ..)

我们运行添加迁移,update-database没有问题,构建完成成功。
但是第一次使用代码优先数据库访问:

  xxxCFContext cfdb = new xxxCFContext(); 

foreach(cfob.Xobjects中的Xobject xobject)

显示错误:



无法找到概念模型类型'xxx.models.yyyclass',但是这个yyyclass存在于edmx中,而不是codefirst部分。



卸载EF5.0,安装EF6.0,错误消失。但我需要EF5.0而不是6.0 alfa3预发行。



有什么问题?我们如何用EF5.0与codefirst混合使用edmx?



我将非常感谢任何想法。



编辑



我知道这个解决方法,但对我来说没有帮助:



- 尽管EF的较新版本可能会解决这个问题(也许这就是为什么EF6 alphas为您工作)。


The situation as follows:

we have one big MVC project with database first approach on EF5.0:

ObjectContext constructor:

namespace xxx.Models
{
    (...)
        public partial class xxxEntities : ObjectContext
        {
            #region Constructors

            /// <summary>
            /// (...)
            /// </summary>
            public xxxEntities() : base("name=xxxEntities", "xxxEntities")
            {
                this.ContextOptions.LazyLoadingEnabled = true;
                OnContextCreated();
            }

    (...)

connection string:

    <add name="xxxEntities" 
         connectionString="metadata=res://*/Models.xxxModel.csdl|
         res://*/Models.xxxModel.ssdl|res://*/Models.xxxModel.msl;
         provider=System.Data.SqlClient;provider connection string=&quot;
         data source=.;Initial catalog=xxxdb;
         integrated security=True;
         multipleactiveresultsets=True;
         App=EntityFramework&quot;"
         providerName="System.Data.EntityClient" />

We choose Code First for testing the new development on separated namespace.

connection string:

<add name="xxxCFContext" 
     connectionString="Data Source=.;
     Initial Catalog=xxxdb;
     Integrated Security=True;
     User Instance=False;
     MultipleActiveResultSets=True" 
     providerName="System.Data.SqlClient" />

DbContext constructor:

namespace xxx.Models.CodeFirst
{
    public partial class xxxCFContext : DbContext
    {
        static xxxCFContext()
        {
            Database.SetInitializer<xxxCFContext>(new ValidateDatabase<xxxCFContext>());
        }

        public xxxCFContext()
            : base("Name=xxxCFContext")
        {
        }

(...)

We run add-migrations, update-database without problem, the build completed with success. But on the first time with the code-first db access:

xxxCFContext cfdb = new xxxCFContext();

foreach (Xobject xobject in cfdb.Xobjects)

Appear the error:

"Could not find the conceptual model type 'xxx.models.yyyclass'", but this yyyclass exist in edmx, NOT the codefirst part.

Uninstall EF5.0, install EF6.0 and the error disappeared. But i need the EF5.0 not the 6.0 alfa3 prerelease.

What's wrong? How can we use edmx mixed with codefirst with EF5.0?

I would be very grateful for any idea.

EDIT

I know this workarounds, but not help for me:

Have anyone used Entity Framework with code first approach mixed with Edmx file?

http://blog.oneunicorn.com/2012/02/26/dont-use-code-first-by-mistake/

Could not find the conceptual model type

http://blog.oneunicorn.com/2012/02/26/dont-use-code-first-by-mistake/

Entity Framework (4.3) looking for singular name instead of plural (when entity name ends with "s")

Building an MVC 3 App with Database First and Entity Framework 4.1

The problem of generating (with xxxmodel.Context.tt and xxxModel.tt) the large existing edmx heavily exploits the advantages of ObjectContext, so we can not simply change from the ObjectContext to the DbContext (behind the existing edmx).

Edit II

From DB First to CodeFirst, we choose the following:

(Right-click into EDMX editor and add code generation item. If you do not seem, to be installed: Tools menu, Extensions and Updates, EF 5.x DbContext Fluent Generator)

  • The model objects and the context is copied from the EDMX.
  • The EDMX was deleted anything that was under him.
  • Set the ConnectionString from was difficult EDMX-style to simple codefirst form.
  • and tadam, in about 10 minutes, we moved from Database First to Code First. During development, with existing 80 tables in db.

  • run Enable-Migrations in Power Management Console, and continued with CodeFirst.

解决方案

It may help to keep the different "types" of EF (code-first, database first, etc.) in separate assemblies: a limitation of EF is that it is not possible use code-first in an assembly with certain database-first attributes - though a newer version of EF may fix this (perhaps that's why EF6 alphas work for you).

这篇关于EDMX用于遗留代码_and_代码首先在一个MVC项目中一起进行新的开发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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