2017-07-07 57 views
0

我在windowformapplication中使用實體框架時,當我調用「db.Persons.Add()」它不顯示引用人和給出了錯誤:錯誤:NBTSol.Models.ContactDbContext.Persons是無法訪問,由於其保護級別-C#

錯誤:NBTSol.Models.ContactDbContext.Persons無法訪問由於其保護級別 即使一切都是公開的,

那麼爲什麼呢?

角色模型:

using System; 
using System.Collections.Generic; 
using System.ComponentModel.DataAnnotations; 
using System.ComponentModel.DataAnnotations.Schema; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace NBTSol.Models 
{ 
    public class Person 
    { 
    [Key] 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public long PersonId { get; set; } 

    [Required(ErrorMessage = "First Name is Required")] 
    public string FirstName { get; set; } 

    [Required(ErrorMessage = "Last Name is Required")] 
    public string LastName { get; set; } 

    [Required(ErrorMessage = "Email is Required")] 
    public string Email { get; set; } 

    [Required(ErrorMessage = "Gender is Required")] 
    public string Gender { get; set; } 

    [Required(ErrorMessage = "Address is Required")] 
    public string Address { get; set; } 

    [Required(ErrorMessage = "City is Required")] 
    public string City { get; set; } 

    [Required(ErrorMessage = "State is Required")] 
    public string State { get; set; } 

    [Required(ErrorMessage = "PostalCode is Required")] 
    public string PostalCode { get; set; } 

    [Required(ErrorMessage = "Country is Required")] 
    public string Country { get; set; } 

    [Required(ErrorMessage = "Contact Number is Required")] 
    public string ContactNumber { get; set; } 

    [Required(ErrorMessage = "CNIC is Required")] 
    public string CNIC { get; set; } 


    public string OrganizationName { get; set; } 
    public string Designation { get; set; } 
    public string Salary { get; set; } 
    public string Description { get; set; } 

    public Nullable<bool> IsClient { get; set; } 
    public DateTime EntryDateTime { get; set; } 
    public Nullable<bool> IsDeleted { get; set; } 

} 
} 

我的上下文類:

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

    namespace NBTSol.Models 
    { 
    public class ContactDbContext :DbContext 
    { 
    public ContactDbContext() : base("name=DefaultConnection") { } 

    DbSet<Person> Persons { get; set; } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     //Configure domain classes using Fluent API here 

     base.OnModelCreating(modelBuilder); 
    } 
    } 
    } 

主要形式:這裏是錯誤,當我嘗試調用 「db.Persons.Add()」]

using NBTSol.Models; 
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace NBTSol 
    { 
    public partial class Main : Form 
    { 
    public Main() 
    { 
     InitializeComponent(); 
    } 

    private void Main_Load(object sender, EventArgs e) 
    { 
     ContactDbContext db = new ContactDbContext(); 
     Person person = new Person(); 
     db.Persons.Add(person); 
    } 
    } 
} 

回答

0

使Person屬性在你的數據庫上下文公共,如下,它將工作。

public DbSet<Person> Persons { get; set; } 

與下面的代碼替換您ContactDbContext類:

public class ContactDbContext: DbContext { 
public ContactDbContext(): base("name=DefaultConnection") {} 

public DbSet <Person> Persons { 
    get; 
    set; 
} 

protected override void OnModelCreating(DbModelBuilder modelBuilder) { 
    //Configure domain classes using Fluent API here 

    //Configure table name 
    modelBuilder.Entity<Persons>() 
    .ToTable("Persons "); 

    base.OnModelCreating(modelBuilder); 
} 
} 
+0

感謝名單buddy..i了... ... – mubsher

+0

我已經更新我的答案。 – Zeeshan

+0

還有另一個問題,我已經宣佈我的模型爲Person,並將其複製到DbContext類中的人員,但是它在數據庫中創建人員表格而不是創建人員,但列與 – mubsher

相關問題