Entity framework core &引用;“没有主键”;添加迁移期间的消息

Entity framework core &引用;“没有主键”;添加迁移期间的消息,entity-framework-core,primary-key,database-migration,entity-framework-migrations,blazor-server-side,Entity Framework Core,Primary Key,Database Migration,Entity Framework Migrations,Blazor Server Side,我做了一个ASP.NETBlazor服务器项目 我创建了一个模型类、一个服务类和一个DbContext类。 我还创建了一个SQL数据库,并在appsettings.json和startup.cs上连接到它 下面是我在Startup.cs中IConfiguration配置{get;}下的代码: services.AddScoped<ClassNameService>(); //connect to business logic #region Connect

我做了一个ASP.NETBlazor服务器项目

我创建了一个模型类、一个服务类和一个DbContext类。 我还创建了一个SQL数据库,并在appsettings.json和startup.cs上连接到它

下面是我在Startup.cs中
IConfiguration配置{get;}
下的代码:

services.AddScoped<ClassNameService>();  //connect to business logic 
            #region Connection String   
            services.AddDbContext<AppDBContext>(item => item.UseSqlServer(Configuration.GetConnectionString("DatabaseName"))); 
            #endregion
Id是此处的主键

以下是DbContext和服务类代码:

public class AppDBContext:DbContext 
    { 
        public AppDBContext(DbContextOptions<AppDBContext> options) : base(options) 
        { 
        } 
        public DbSet<ContactPerson> ContactPeople { get; set; } 
    } 
公共类AppDBContext:DbContext
{ 
公共AppDBContext(DbContextOptions选项):基本(选项)
{ 
} 
公共数据库集联系人{get;set;}
} 
公共类ContactPersonService
{ 
#区域属性
私有只读AppDBContext_AppDBContext;
#端区
#区域构造函数
公共联系人服务(AppDBContext AppDBContext)
{ 
_appDBContext=appDBContext;
} 
#端区
#区域获取联系人列表
公共异步任务GetAllContactPeopleSync()
{ 
return wait_appDBContext.ContactPeople.toListSync();
} 
#端区
#地区插入联系人
公共异步任务InsertContactPersonAsync(联系人联系人联系人)
{ 
wait_appDBContext.ContactPeople.AddAsync(contactperson);
wait_appDBContext.saveChangesSync();
返回true;
} 
#端区
#区域通过Id获取联系人
公共异步任务GetContactPersonalsync(int-Id)
{ 
ContactPerson ContactPerson=wait_appDBContext.ContactPeople.FirstOrDefaultAsync(c=>c.Id.Equals(Id));
返回联系人;
} 
#端区
#地区更新联系人
公共异步任务UpdateContactPersonAsync(ContactPerson-ContactPerson)
{ 
_appDBContext.ContactPeople.Update(联系人);
wait_appDBContext.saveChangesSync();
返回true;
} 
#端区
#地区联系人
公共异步任务DeleteContactPersonAsync(ContactPerson-ContactPerson)
{ 
_appDBContext.Remove(联系人);
wait_appDBContext.saveChangesSync();
返回true;
} 
#端区
} 

那么,为什么我会收到此错误消息以及如何修复此错误?

您的类包含一些无效属性。不能将数据注释属性用作特性类型。作为第一种方法,您可以用字符串替换属性:

public class ContactPerson 
    { 
        [Key] 
        public int Id { get; set; }
        public string Title { get; set; } 
        public string FirstName { get; set; } 
        public string LastName { get; set; } 
        public string Designation { get; set; } 
        public string Company { get; set; }
        [Phone]
        public string MobilePhoneNumber { get; set; } 
        [Phone]
        public string LandlinePhoneNumber { get; set; } 
        [EmailAddress]
        public string Email { get; set; } 
        public string Address { get; set; } 
    }
````
or you can try this validation for the phones instead of above one
````
[Display(Name = "Your contact number :")]
[Required(ErrorMessage = "A phone number is required.")]
[DataType(DataType.PhoneNumber, ErrorMessage = "Invalid Phone Number")]
[RegularExpression(@"^([0-9]{10})$", ErrorMessage = "Invalid Phone Number.")]
````

您的类包含一些无效属性。不能将数据注释属性用作特性类型。作为第一种方法,您可以用字符串替换属性:

public class ContactPerson 
    { 
        [Key] 
        public int Id { get; set; }
        public string Title { get; set; } 
        public string FirstName { get; set; } 
        public string LastName { get; set; } 
        public string Designation { get; set; } 
        public string Company { get; set; }
        [Phone]
        public string MobilePhoneNumber { get; set; } 
        [Phone]
        public string LandlinePhoneNumber { get; set; } 
        [EmailAddress]
        public string Email { get; set; } 
        public string Address { get; set; } 
    }
````
or you can try this validation for the phones instead of above one
````
[Display(Name = "Your contact number :")]
[Required(ErrorMessage = "A phone number is required.")]
[DataType(DataType.PhoneNumber, ErrorMessage = "Invalid Phone Number")]
[RegularExpression(@"^([0-9]{10})$", ErrorMessage = "Invalid Phone Number.")]
````

你们只有一张桌子吗?为什么您认为是联系人导致了错误?例如,我看到您有PhoneAttribute。这是一个枚举吗?@Serge一旦这部分工作正常,我将添加更多的表。我想问题可能是个人的,但我不确定。PhoneAttribute是System.ComponentModel.DataAnnotations命名空间中的一个类。如果PhoneAttribute是一个类,则需要修复ContactPerson类。这是一个错误。请把剩下的课发出去。@Serge我发了。谢谢,但我说的是课。您的数据库上下文中是否只有公共数据库集联系人而没有其他联系人?很抱歉,我必须再次询问PhoneAttribute是枚举还是类?什么东西只有一个表?为什么您认为是联系人导致了错误?例如,我看到您有PhoneAttribute。这是一个枚举吗?@Serge一旦这部分工作正常,我将添加更多的表。我想问题可能是个人的,但我不确定。PhoneAttribute是System.ComponentModel.DataAnnotations命名空间中的一个类。如果PhoneAttribute是一个类,则需要修复ContactPerson类。这是一个错误。请把剩下的课发出去。@Serge我发了。谢谢,但我说的是课。您的数据库上下文中是否只有公共数据库集联系人而没有其他联系人?抱歉,我必须再次询问PhoneAttribute是枚举还是类?我尝试了第一个选项,现在它可以工作了。我能够创建迁移。ThanksI尝试了第一个选项,现在可以使用了。我能够创建迁移。谢谢
public class ContactPerson 
    { 
        [Key] 
        public int Id { get; set; }
        public string Title { get; set; } 
        public string FirstName { get; set; } 
        public string LastName { get; set; } 
        public string Designation { get; set; } 
        public string Company { get; set; }
        [Phone]
        public string MobilePhoneNumber { get; set; } 
        [Phone]
        public string LandlinePhoneNumber { get; set; } 
        [EmailAddress]
        public string Email { get; set; } 
        public string Address { get; set; } 
    }
````
or you can try this validation for the phones instead of above one
````
[Display(Name = "Your contact number :")]
[Required(ErrorMessage = "A phone number is required.")]
[DataType(DataType.PhoneNumber, ErrorMessage = "Invalid Phone Number")]
[RegularExpression(@"^([0-9]{10})$", ErrorMessage = "Invalid Phone Number.")]
````