实体框架代码生成策略会产生错误

时间:2013-10-19 21:18:07

标签: c# entity-framework

我安装了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'的定义,也没有扩展方法'ContextOptions'接受类型'WpfApplication1.SchoolDBEntities'的第一个参数(你是否缺少using指令或汇编引用? )

有谁知道为什么它会生成错误的代码?

2 个答案:

答案 0 :(得分:0)

您需要下载 适用于Visual Studio 2012的实体框架6工具&amp; 2013

http://www.microsoft.com/en-us/download/confirmation.aspx?id=40762

如描述 Entity Framework Designer Errors

答案 1 :(得分:0)

在Entity Framwork6中,您可以使用旧版上下文。只需右键单击edmx文件,然后在打开时使用选项在XML查看器中打开它。然后搜索遗留并将其值设置为true。