简体   繁体   English

使用流利的API内部类的配置实体框架表

[英]Configuration Entity framework table using fluent API internal class

I have a class and I want to generate a table in database using this class. 我有一个类,我想使用该类在数据库中生成一个表。 If I use separate class to configuration this table it works correctly when I want to Configuration this table with two column as key using internal class it has an error: 如果我使用单独的类来配置此表,则当我想使用内部类使用两列作为键来配置此表时,它可以正常工作,它会出现错误:

one or more validation errors were detected during model generation. 在模型生成过程中检测到一个或多个验证错误。 StoreKabir.Models.Company :: entitytype 'Company' has no key defined. StoreKabir.Models.Company ::实体类型'Company'尚未定义键。 Defined the key for this EntityType 定义了此EntityType的键

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StoreKabir.Models
{
    class Company
    {
        internal class Configuration : System.Data.Entity.ModelConfiguration.EntityTypeConfiguration<Company>
        {
            public Configuration()
            {
                #region TableCompanyConfiguration

                ToTable("Companies");
                HasKey(current => new
                {
                    current.CopmanyId,
                    current.CompanyName
                });

                Property(current => current.CopmanyId)
                    .HasColumnName("CopmanyId")
                    .HasColumnOrder(0)
                    .IsRequired()
                    .HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.None);

                Property(current => current.CompanyName)
                    .HasColumnName("CompanyName")
                    .HasColumnOrder(1)
                    .IsRequired()
                    .IsVariableLength()
                    .IsUnicode(true)
                    .HasMaxLength(40);

                #endregion TableCompanyConfiguration
            }
        }

        public Company()
        {

        }

        public Company(Int64 companyId, string companyName)
        {

            CopmanyId = companyId;

            CompanyName = companyName;
        }

        public Int64 CopmanyId { get; set; }

        public string CompanyName { get; set; }
    }
}

Seems to me you need to put an attribute [Key] on your first property, CompanyId. 在我看来,您需要在第一个属性CompanyId上放置一个[Key]属性。

Not sure if EF can get it from context if it is not named Id. 如果EF没有命名为Id,则不确定EF是否可以从上下文中获取它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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