我安装了 Entity Framework 6 并使用 Visual Studio 2012 进行练习。我创建了一个 WPF 应用程序并添加了一个 ADO.NET 实体数据模型来连接到我在 localhost 上的 MySQL 数据库。在设计(数据模型)的属性中,我将“代码生成策略”的值更改为“默认”,以便它Model1.Designer.cs
为我生成代码。但它生成的代码有错误:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </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()]
#region EDM Relationship Metadata
[assembly: EdmRelationshipAttribute("SchoolDBModel", "studentcourse", "course", System.Data.Metadata.Edm.RelationshipMultiplicity.Many, typeof(WpfApplication1.course), "student", System.Data.Metadata.Edm.RelationshipMultiplicity.Many, typeof(WpfApplication1.student))]
#endregion
namespace WpfApplication1
{
#region Contexts
/// <summary>
/// No Metadata Documentation available.
/// </summary>
public partial class SchoolDBEntities : ObjectContext
{
#region Constructors
/// <summary>
/// Initializes a new SchoolDBEntities object using the connection string found in the 'SchoolDBEntities' section of the application configuration file.
/// </summary>
public SchoolDBEntities() : base("name=SchoolDBEntities", "SchoolDBEntities")
{
this.ContextOptions.LazyLoadingEnabled = true;
OnContextCreated();
}
// More code below with errors...
第一个错误是构造函数中的基本调用,即
'System.Data.Entity.DbContext.DbContext(string, System.Data.Entity.Infrastructure.DbCompiledModel)' 的最佳重载方法匹配有一些无效参数
下一个错误是它设置的行this.ContextOptions.LazyLoadingEnabled
,它说
“WpfApplication1.SchoolDBEntities”不包含“ContextOptions”的定义,并且找不到接受“WpfApplication1.SchoolDBEntities”类型的第一个参数的扩展方法“ContextOptions”(您是否缺少 using 指令或程序集引用?)
有谁知道为什么它会生成有错误的代码?