c#把目标框架从.net framework4.5改成4出错
本帖最后由 realdrizzt 于 2014-01-20 08:12:25 编辑 错误1“WindowsFormsApplication2.Model1Container”的分部声明一定不能指定不同的基类D:\autofill\WindowsFormsApplication2\Model1.Context.cs1626WindowsFormsApplication2
错误2类型“WindowsFormsApplication2.Model1Container”已定义了一个名为“Model1Container”的具有相同参数类型的成员D:\autofill\WindowsFormsApplication2\Model1.Designer.cs3416WindowsFormsApplication2
这是Model1.Context.cs文件内容
//------------------------------------------------------------------------------
// <auto-generated>
// 此代码是根据模板生成的。
//
// 手动更改此文件可能会导致应用程序中发生异常行为。
// 如果重新生成代码,则将覆盖对此文件的手动更改。
// </auto-generated>
//------------------------------------------------------------------------------
namespace WindowsFormsApplication2
{
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class Model1Container : DbContext
{
public Model1Container()
: base("name=Model1Container")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
}
}
这是Model1.Designer.cs文件内容
//------------------------------------------------------------------------------
// <auto-generated>
// 此代码是根据模板生成的。
//
// 手动更改此文件可能会导致应用程序中发生异常行为。
// 如果重新生成代码,则将覆盖对此文件的手动更改。
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.ComponentModel;
using System.Data.EntityClient;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Linq;
using System.Runtime.Serialization;
using System.Xml.Serialization;
[assembly: EdmSchemaAttribute()]
namespace WindowsFormsApplication2
{
#region 上下文
/// <summary>
/// 没有元数据文档可用。
/// </summary>
public partial class Model1Container : ObjectContext
{
#region 构造函数
/// <summary>
/// 请使用应用程序配置文件的“Model1Container”部分中的连接字符串初始化新 Model1Container 对象。
/// </summary>
public Model1Container() : base("name=Model1Container", "Model1Container")
{
this.ContextOptions.LazyLoadingEnabled = true;
OnContextCreated();
}
/// <summary>
/// 初始化新的 Model1Container 对象。
/// </summary>
public Model1Container(string connectionString) : base(connectionString, "Model1Container")
{
this.ContextOptions.LazyLoadingEnabled = true;
OnContextCreated();
}
/// <summary>
/// 初始化新的 Model1Container 对象。
/// </summary>
public Model1Container(EntityConnection connection) : base(connection, "Model1Container")
{
this.ContextOptions.LazyLoadingEnabled = true;
OnContextCreated();
}
#endregion
#region 分部方法
partial void OnContextCreated();
#endregion
}
#endregion
}
编辑器用的vs2012
[解决办法]
Model1.Context.cs文件中using 为什么会在命名空间里边?
[解决办法]
一个继承子ObjectContext,一个继承子DbContext,但是他们是同一个类,因此出错
[解决办法]
尽量不要修改Model1.Designer.cs中的内容,你将DbContext那个修改为ObjectContext,不就可以了吗?
ObjectContext是一种模型优先的开发模式,DbContext是代码优先的开发模式,这两个概念你搜索了解下吧
[解决办法]
DbContext和ObjectContext二选一,两处类定义只能使用其中之一,我不知道你这两个父类是谁继承了谁,猜想大概是DbContext继承了ObjectContext吧,如果是那样,就把Model1.Designer.cs里面的ObjectContext改为DbContext,使用子类的话,方法属性更多,不会出错。