我在windowformapplication中使用实体框架,当我调用" db.Persons.Add()"它没有显示人员的推荐并给出错误:
错误:由于其保护级别,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);
}
}
}
主要形式:[这是我尝试调用时的错误&#34; db.Persons.Add()&#34;]
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 :(得分: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);
}
}