问题描述
大家好,我有一个C#WPF项目,我使用ADO.net数据模型为其添加了Entity Framework。我有一个.mdf数据库,里面有一个表和数据。当我调用data.ToList()方法时,我从数据库中获取正确的数据到我的数据网格中。这是正确的。我遇到的问题是当我创建/插入新文件到数据库时,记录不会被保存。我也得到0错误。我在网上阅读了几个小时试图解决这个问题,到目前为止,我想出的可能是因为我的连接字符串,因为我有|数据目录|或类似的东西可能呢?当我将所选用户发送到我创建的下一个屏幕时,我还可以将用户加载到屏幕中。只是无法弄清楚为什么我的用户不会保存到数据库。
Hello all I have a C# WPF project and I added Entity Framework to it with a ADO.net data model. I have a .mdf database with a table in it and data. When I call the data.ToList() method I get the correct data from my db into my datagrid. So that is correct. The issue I am having is when I go to create/insert a new file into the database the record does not get saved. I also get 0 errors. I been reading online for hours trying to figure this out and so far all I came up with is maybe its because of my connection string because I have |data directory| or something like that it is maybe? I can also load the user into the screen when I send the selected user to the next screen I have created. Just cannot figure out why my user will not save to the database.
//connstring –
<connectionStrings>
<add name="EFcontext" connectionString="metadata=res://*/EFModel.csdl|res://*/EFModel.ssdl|res://*/EFModel.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDB)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\EntityFramewordDB.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
public class CRUD
{
//get the entity framework model context that was created.
private EFcontext framework = null;
public CRUD()
{
framework = new EFcontext();
}
public Person GetPersonById(int personId)
{
return framework.People.Find(personId);
}
public List<Person> GetAllPeopleByList()
{
return framework.People.ToList();
}
public void CreatePerson(Person person)
{
if(person != null)
{
framework.People.Add(person);
framework.SaveChanges();
}
} public partial class MainWindow : Window
{
private CRUD entityFramework;
private Person dataGridPerson;
public MainWindow()
{
InitializeComponent();
entityFramework = new CRUD();
PopulateDataGrid();
}
private void PopulateDataGrid()
{
DataGridPeople.ItemsSource = entityFramework.GetAllPeopleByList();
}
private void BtnCreate_Click(object sender, RoutedEventArgs e)
{
Window CreatePerson = new CreateUpdateWindow();
CreatePerson.Show();
Close();
} public partial class CreateUpdateWindow : Window
{
private CRUD entityFramework;
private Person personWindowObject; public CreateUpdateWindow()
{
InitializeComponent();
entityFramework = new CRUD();
private void BtnSave_Click(object sender, RoutedEventArgs e)
{
Person personObject = new Person();
Window mainWindow = new MainWindow(); DateTime dateNow = DateTime.Now;
personObject.DateCreated = dateNow;
entityFramework.CreatePerson(personObject);
MessageBox.Show("Created new person.", "Created");
mainWindow.Show();
Close();
}
}
//copied all sections of my code what is being used for creating user and to add 2 db
我尝试过:
在网上看了几个小时尝试不同的方式可能会出现问题不确定?
What I have tried:
looking online for hours trying different ways maybe connstring problem unsure?
推荐答案
private void BtnSave_Click(object sender, RoutedEventArgs e)
{
Person personObject = new Person();
DateTime dateNow = DateTime.Now;
personObject.DateCreated = dateNow;
// TODO: You probably want to set some other properties here...
entityFramework.CreatePerson(personObject);
MessageBox.Show("Created new person.", "Created");
Window mainWindow = new MainWindow();
mainWindow.Show();
Close();
}
这篇关于C#WPF项目实体框架不会在DB中添加记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!