Skip to main content
 首页 » 编程设计

c#之不一致的可访问性错误

2024年02月06日12dflying

我的错误:

Error 1 Inconsistent accessibility: property type 'System.Data.Entity.DbSet<MvcMusicStore.Models.OrderDetail>' is less accessible than property 'MvcMusicStore.Models.MusicStoreEntities.OrderDetails' C:\Users\Jose\documents\visual studio 2013\Projects\MvcMusicStore\Models\MusicStoreEntities.cs 19 35 MvcMusicStore

Error 2 Inconsistent accessibility: property type 'System.Collections.Generic.List<MvcMusicStore.Models.OrderDetail>' is less accessible than property 'MvcMusicStore.Models.Order.OrderDetails' C:\Users\Jose\documents\visual studio 2013\Projects\MvcMusicStore\Models\Order.cs 25 32 MvcMusicStore

我的代码导致了这些错误:

public class Order 
{ 
    public int OrderId { get; set; } 
    public string Username { get; set; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public string Address { get; set; } 
    public string City { get; set; } 
 
    public string State { get; set; } 
    public string PostalCode { get; set; } 
    public string Country { get; set; } 
    public string Phone { get; set; } 
    public string Email { get; set; } 
    public decimal Total { get; set; } 
    public System.DateTime OrderDate { get; set; } 
 
    public List<OrderDetail> OrderDetails { get; set; }<<<<<ERROR 

另一个错误在

public class MusicStoreEntities:DbContext 
{ 
    public DbSet<Album> Albums { get; set; } 
    public DbSet<Genre> Genres { get; set; } 
    public DbSet<Artist> Artist { get; set; } 
    public DbSet<Cart> Carts { get; set; } 
    public DbSet<Order> Orders { get; set; } 
    public DbSet<OrderDetail> OrderDetails { get; set; }<<<<<<ERROR 
 
    public System.Data.Entity.DbSet<MvcMusicStore.Models.Artist> Artists { get; set; } 
} 

请您参考如下方法:

这两个错误意味着同一件事:泛型类型 List 和 DbSet 的类型参数,即 OrderDetail 和 MvcMusicStore。 Models.Artist 未在您的代码中标记为公开,从而使它们在您的包内部可见。这意味着这些类型不能在 public 属性中使用。

有两种方法可以解决这个问题:

  • 您可以将类型设为public,或者
  • 您可以将属性设为内部。

选择哪种操作取决于项目中 OrderDetails 和 Artists 的可访问性要求。