I have some data from an XML file that I have transposed and I can map to my Entities. So now I want to save them all to the database .
I was reading on SO Can Entity Framework add many related entities with single SaveChanges()?
The accepted answer does not have too much info but this statement:
'You don`t need to save changes every time if you use objects
references to newly created objects not IDs:'
My entities are derived from a dataset tables which all have a related Id columns.
And I guessing the answer is more or less that the related entity was created with something like this : item.SubItem = new SubItem();
rather than item.SubId = SubItem.Id;
So should I traverse my data tables and translate my dataset tables into Entities by creating the objects from the tables and adding them to the context.
So for each row in dt['Items'] if row has a subitem new SubItem {all values except the Id}..??
Any example code would be appreciated.
So after working to resolve the issue I discovered if I create a new object of my entity and map values to it excluding the ID column I can then assign this object back to the parent.
pseudo code is below:
myobject = new myobject(){ param1 = oldobject.param1}
myentityParent.ChildTableEntity.Add(myobject);
Related
How can I get data from only one table in relational tables in entity framework?
I had created two tables. One is Classes and other is Students, and classes has one-to-many students relationship, but when I try to get data, it returns data from students and from classes tables.
(System.Data.Entity.DynamicProxies.Class_A4175BA8B7189ED27663CF9F9601DDF445EED57DA923B995D31056ABB560F13A).
How can I remove this columnn?
Code:
TestDbEntities entities = new TestDbEntities();
dataGridView1.DataSource = entities.Students.ToList();
Not familiar with entity framework but the object you have listed there is a proxy object
System.Data.Entity.DynamicProxies.Class_A4175BA8B7189ED27663CF9F9601DDF445EED57DA923B995D31056ABB560F13A
That's why it has that strange name. If you trace your database call I would think that the query is not joining to the class table. Instead the proxy is generated to enable lazy loading. So if you never touch the class object, it is never grabbed from the DB.
(Using: Visual Studio 2010, SQL Server 2012, Entity Framework 4.0, MVC3 web application)
I have tables with one-to-many and many-to-many relationships, I used (database first) to automatically generate model classes, meaning I have objects inherited from EntityObject and the base class inherited from ObjectContext.
I am having trouble with (inserting, updating, deleting) objects.
For example when creating an object: I have a many-to-many relationship between 2 tables (Area and Cell) and the middle table (CellArea), and I want to add a new Cell object which is connected to many areas, so I did the following code:
Cell _cell = new Cell()
foreach (Area ar in current_areas)
{
var ca = new CellArea();
//ca attributes
_cell.CellAreas.Add(ca);
}
db.SaveChanges();
db.Cells.AddObject(_cell);
db.SaveChanges();
I tried other code snippets and none of them worked, I always get IEntityChangeTracker and other similar exceptions.
What is the proper way to manage such cases?
When there are intertable relationships EF should create what is known as navigational properties. When you perform the initial query you can either choose to have those properties loaded or not. In particular EF has a keyword named Include that allows you to populate those other properties at will. If all the other date is included and any changes are made, you only have to call the SaveChanges method and the original table data and all navigational property data is saved in one shot.
Google "EF Include"
I connected to DataBase with Entity:
Entity db = new Entity();
...
Then i add DataGrid to Form and try to recive table from my DataBase
var pp = from asd in db.ABONENT select asd;
MyDataGrid.ItemsSource = pp.ToList();
The result is here:
Screenshot
it's display other fields from other linked tables, why ?
How to display data only from ABONENT table?
My guess is that you are using a DataGrid to display the content of all your tables.
Are you columns definition static or dynamically loaded?
If it's dynamically, I suggest to Remove all columns between every Data Binding.
If it's static, hide the columns you don't wanna display (Visible = false).
In Entity Framework, you have Entities, not Tables. Entity Framework abstracts the relational concept of tables into objects you use within your application. That's what an ORM does.
Because of this, relations between tables are expressed as what is called a Navigation Property in your entities, which is basically a property inside the entity class that represents the associated entity.
My point is.. why do you use an ORM if you intend to expose the tables directly into the UI?. Use plain old ADO.Net for that, or otherwise define your UI in such a way that you don't expose the entire table directly to the user. The user knows nothing about tables. The user understands their business. Therefore your application should know nothing about tables.
I see this as a bad practice from a UX perspective, for example, why should the user see the Id columns such as abonentID and RegionID into their UI?? they don't care about that, nor do they understand that. row IDs are a RDBMS concept, not a business concept.
My suggestion: Rethink your approach: either fallback to using plain old ADO.Net, or set the AutoGenerateColumns to false in the DataGrid and expose only the columns the user cares about.
You can select the exposed properties of the entities by using the following syntax:
var pp = from asd in db.Products
select new
{
asd.Id,
asd.Name,
ProductCategory = asd.ProductCategory.Name,
};
MyDataGrid.ItemsSource = pp.ToList();
I'm pretty new to Entity Frameworks.
I have a set of Static Data which includes the following:
Templates, which have a Many To Many relationship with Columns
I want to create a new UserReport which has a reference to an existing Template, and a subset of the Columns associated with the said same template.
I expect to save the UserReport record, and a series of records in a UserReportColumns mapping table linking the UserReport to existing columns.
My code is as follows:
ReportTemplate template = staticData.ReportTemplates.First();
UserReport newReport = new UserReport()
{
....
Columns = FilterColumns(template.Columns)
....
}
....
context.UserReports.Attach(userReport);
context.UserReports.AddObject(userReport);
context.SaveChanges();
This attempts to recreate everything in the object hierarchy in the database.
How can I get it so that it only saves the UserReport and the UserReport_Column links?
You are retrieving the template from a different DbContext instance(or some cached data source). Hence the context assumes that the template and the columns are new objects.
To overcome this problem you have to attach the existing objects to the context to avoid EF adding them to the database.
foreach(var column in newReport.Columns)
{
context,Column.Attach(column);
}
context.UserReports.Attach(template);
context.UserReports.AddObject(userReport);
context.SaveChanges();
Suppose I have a table "Client" and a table "Contact". If I wanted to copy a Client row into my database along with new entries for Contacts (I don't want pointers to the already existing table) how could I accomplish this in a recursive way? I'd hate to have to go down to every table that "touches" these tables and manually create new rows with the same data, but new ID's. Thanks for your help.
You could write a partial class for you entity that implements ICloneable. Then, in your Clone() method, you can contain the logic for cloning that entity and its related entities.