Entity Framework Core 2.1 : The specified field 'Model' could not be found for property 'Model' on entity...












1















I need to use Code First approach to generate Database by Entity Framework Core 2.1 but I get this error:



The specified field 'Model' could not be found for property 'Model' on entity type 'BarCodeDevice'.


Here are the classes I have used to do so



public class BarCodeDevice
{
public int SerialNumber { get; set; }
public string Model { get; set; }
public virtual ICollection<ClientBarCodeDevice> ClientBarCodeDeviceList { get; set; }
}


And the configuration class



public class BarCodeDeviceConfiguration : IEntityTypeConfiguration<BarCodeDevice>
{
public void Configure(EntityTypeBuilder<BarCodeDevice> builder)
{
builder.HasKey(x => x.SerialNumber);
builder.Property(t => t.Model)
.IsRequired()
.HasField("Model");
}
}


And The DbContext Class



public class SegregationDbContext : DbContext, IDisposable
{
public SegregationDbContext(DbContextOptions<SegregationDbContext> options) : base(options)
{ }


protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new BarCodeDeviceConfiguration());
}

public DbSet<BarCodeDevice> BarCodeDevices { get; set; }
}


And finally the configuration



public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<SegregationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("Default")));
services.AddMvc();
}









share|improve this question



























    1















    I need to use Code First approach to generate Database by Entity Framework Core 2.1 but I get this error:



    The specified field 'Model' could not be found for property 'Model' on entity type 'BarCodeDevice'.


    Here are the classes I have used to do so



    public class BarCodeDevice
    {
    public int SerialNumber { get; set; }
    public string Model { get; set; }
    public virtual ICollection<ClientBarCodeDevice> ClientBarCodeDeviceList { get; set; }
    }


    And the configuration class



    public class BarCodeDeviceConfiguration : IEntityTypeConfiguration<BarCodeDevice>
    {
    public void Configure(EntityTypeBuilder<BarCodeDevice> builder)
    {
    builder.HasKey(x => x.SerialNumber);
    builder.Property(t => t.Model)
    .IsRequired()
    .HasField("Model");
    }
    }


    And The DbContext Class



    public class SegregationDbContext : DbContext, IDisposable
    {
    public SegregationDbContext(DbContextOptions<SegregationDbContext> options) : base(options)
    { }


    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
    modelBuilder.ApplyConfiguration(new BarCodeDeviceConfiguration());
    }

    public DbSet<BarCodeDevice> BarCodeDevices { get; set; }
    }


    And finally the configuration



    public void ConfigureServices(IServiceCollection services)
    {
    services.AddDbContext<SegregationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("Default")));
    services.AddMvc();
    }









    share|improve this question

























      1












      1








      1








      I need to use Code First approach to generate Database by Entity Framework Core 2.1 but I get this error:



      The specified field 'Model' could not be found for property 'Model' on entity type 'BarCodeDevice'.


      Here are the classes I have used to do so



      public class BarCodeDevice
      {
      public int SerialNumber { get; set; }
      public string Model { get; set; }
      public virtual ICollection<ClientBarCodeDevice> ClientBarCodeDeviceList { get; set; }
      }


      And the configuration class



      public class BarCodeDeviceConfiguration : IEntityTypeConfiguration<BarCodeDevice>
      {
      public void Configure(EntityTypeBuilder<BarCodeDevice> builder)
      {
      builder.HasKey(x => x.SerialNumber);
      builder.Property(t => t.Model)
      .IsRequired()
      .HasField("Model");
      }
      }


      And The DbContext Class



      public class SegregationDbContext : DbContext, IDisposable
      {
      public SegregationDbContext(DbContextOptions<SegregationDbContext> options) : base(options)
      { }


      protected override void OnModelCreating(ModelBuilder modelBuilder)
      {
      modelBuilder.ApplyConfiguration(new BarCodeDeviceConfiguration());
      }

      public DbSet<BarCodeDevice> BarCodeDevices { get; set; }
      }


      And finally the configuration



      public void ConfigureServices(IServiceCollection services)
      {
      services.AddDbContext<SegregationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("Default")));
      services.AddMvc();
      }









      share|improve this question














      I need to use Code First approach to generate Database by Entity Framework Core 2.1 but I get this error:



      The specified field 'Model' could not be found for property 'Model' on entity type 'BarCodeDevice'.


      Here are the classes I have used to do so



      public class BarCodeDevice
      {
      public int SerialNumber { get; set; }
      public string Model { get; set; }
      public virtual ICollection<ClientBarCodeDevice> ClientBarCodeDeviceList { get; set; }
      }


      And the configuration class



      public class BarCodeDeviceConfiguration : IEntityTypeConfiguration<BarCodeDevice>
      {
      public void Configure(EntityTypeBuilder<BarCodeDevice> builder)
      {
      builder.HasKey(x => x.SerialNumber);
      builder.Property(t => t.Model)
      .IsRequired()
      .HasField("Model");
      }
      }


      And The DbContext Class



      public class SegregationDbContext : DbContext, IDisposable
      {
      public SegregationDbContext(DbContextOptions<SegregationDbContext> options) : base(options)
      { }


      protected override void OnModelCreating(ModelBuilder modelBuilder)
      {
      modelBuilder.ApplyConfiguration(new BarCodeDeviceConfiguration());
      }

      public DbSet<BarCodeDevice> BarCodeDevices { get; set; }
      }


      And finally the configuration



      public void ConfigureServices(IServiceCollection services)
      {
      services.AddDbContext<SegregationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("Default")));
      services.AddMvc();
      }






      c#-4.0 entity-framework-core






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 25 '18 at 10:14









      amin mohammadiamin mohammadi

      828




      828
























          1 Answer
          1






          active

          oldest

          votes


















          4














          The problem is this fluent configuration line:



          .HasField("Model")


          HasField is used to specify the backing field for the property being configured, when the backing field name does not conform to the conventions.



          But you Model property is auto property and there is no backing field called Model, hence the exception.



          So either remove that line, e.g.



          builder.Property(t => t.Model)
          .IsRequired();


          or if you want to force using the backing field with unknown name (which is the case with auto properties), use UsePropertyAccessMode method instead, e.g.



          builder.Property(t => t.Model)
          .IsRequired()
          .UsePropertyAccessMode(PropertyAccessMode.Field);





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466495%2fentity-framework-core-2-1-the-specified-field-model-could-not-be-found-for-p%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            4














            The problem is this fluent configuration line:



            .HasField("Model")


            HasField is used to specify the backing field for the property being configured, when the backing field name does not conform to the conventions.



            But you Model property is auto property and there is no backing field called Model, hence the exception.



            So either remove that line, e.g.



            builder.Property(t => t.Model)
            .IsRequired();


            or if you want to force using the backing field with unknown name (which is the case with auto properties), use UsePropertyAccessMode method instead, e.g.



            builder.Property(t => t.Model)
            .IsRequired()
            .UsePropertyAccessMode(PropertyAccessMode.Field);





            share|improve this answer




























              4














              The problem is this fluent configuration line:



              .HasField("Model")


              HasField is used to specify the backing field for the property being configured, when the backing field name does not conform to the conventions.



              But you Model property is auto property and there is no backing field called Model, hence the exception.



              So either remove that line, e.g.



              builder.Property(t => t.Model)
              .IsRequired();


              or if you want to force using the backing field with unknown name (which is the case with auto properties), use UsePropertyAccessMode method instead, e.g.



              builder.Property(t => t.Model)
              .IsRequired()
              .UsePropertyAccessMode(PropertyAccessMode.Field);





              share|improve this answer


























                4












                4








                4







                The problem is this fluent configuration line:



                .HasField("Model")


                HasField is used to specify the backing field for the property being configured, when the backing field name does not conform to the conventions.



                But you Model property is auto property and there is no backing field called Model, hence the exception.



                So either remove that line, e.g.



                builder.Property(t => t.Model)
                .IsRequired();


                or if you want to force using the backing field with unknown name (which is the case with auto properties), use UsePropertyAccessMode method instead, e.g.



                builder.Property(t => t.Model)
                .IsRequired()
                .UsePropertyAccessMode(PropertyAccessMode.Field);





                share|improve this answer













                The problem is this fluent configuration line:



                .HasField("Model")


                HasField is used to specify the backing field for the property being configured, when the backing field name does not conform to the conventions.



                But you Model property is auto property and there is no backing field called Model, hence the exception.



                So either remove that line, e.g.



                builder.Property(t => t.Model)
                .IsRequired();


                or if you want to force using the backing field with unknown name (which is the case with auto properties), use UsePropertyAccessMode method instead, e.g.



                builder.Property(t => t.Model)
                .IsRequired()
                .UsePropertyAccessMode(PropertyAccessMode.Field);






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 25 '18 at 11:19









                Ivan StoevIvan Stoev

                106k782135




                106k782135
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466495%2fentity-framework-core-2-1-the-specified-field-model-could-not-be-found-for-p%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Costa Masnaga

                    Fotorealismo

                    Sidney Franklin