简体   繁体   中英

Entity Framework Code Generation Strategy generates errors

I installed Entity Framework 6 and am using Visual Studio 2012 to practice with it. I created a WPF application and added a ADO.NET Entity Data Model to connect to a MySQL database that I have on localhost. In the properties of the design (of the data model), I changed to the value of "Code Generation Strategy" to "default," so that it generates code in Model1.Designer.cs for me. But the code that it generates has errors:

    //------------------------------------------------------------------------------
    // <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...

The first error is the base call in the constructor, that says

The best overloaded method match for 'System.Data.Entity.DbContext.DbContext(string, System.Data.Entity.Infrastructure.DbCompiledModel)' has some invalid arguments

And the next error is the line where it's setting this.ContextOptions.LazyLoadingEnabled , it says

'WpfApplication1.SchoolDBEntities' does not contain a definition for 'ContextOptions' and no extension method 'ContextOptions' accepting a first argument of type 'WpfApplication1.SchoolDBEntities' could be found (are you missing a using directive or an assembly reference?)

Does anyone know why it's generating code with errors?

You need to download Entity Framework 6 Tools for Visual Studio 2012 & 2013

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

as described in Entity Framework Designer Errors

In Entity Framwork6 you can use legacy context. Just right click the edmx file and in open with option open it in XML viewer. Then search for legacy and set the value of it as true.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM