簡體   English   中英

使用流利的API內部類的配置實體框架表

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

我有一個類,我想使用該類在數據庫中生成一個表。 如果我使用單獨的類來配置此表,則當我想使用內部類使用兩列作為鍵來配置此表時,它可以正常工作,它會出現錯誤:

在模型生成過程中檢測到一個或多個驗證錯誤。 StoreKabir.Models.Company ::實體類型'Company'尚未定義鍵。 定義了此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; }
    }
}

在我看來,您需要在第一個屬性CompanyId上放置一個[Key]屬性。

如果EF沒有命名為Id,則不確定EF是否可以從上下文中獲取它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM