need guidelines in creating/updating related entities (is it possible with automapper?) - c#

I'm new in ASP.NET MVC and would love to improve here. I used ASP.NET MVC + EF Code first approach. But I'm a little confuse on how to create/update related entites. So here's my scenario. Say,
public class Item
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Stock> Stocks { get; set; }
}
public class Stock
{
public int Id { get; set; }
public int ItemId { get; set; }
public int StorageId { get; set; }
public float Amount { get; set; }
public virtual Item Item { get; set; }
public virtual Storage Storage { get; set; }
}
public class Storage
{
public int Id { get; set; }
public Name { get; set; }
public virtual ICollection<Stock> Stocks { get; set; }
}
So an Item has a 1:many relationship with Stock. And Storage has 1:many relationship with Stock
In displaying them I used Automapper which worked perfectly. (Thanks to SO for helping me)
Now, what I'm trying to achieve is.. How to create/update entites? (Is it possible to used Automapper here?)
Say, in one POST it will add an Item, with Stock, and with selected Storage. A sample code would be great for reference.
Any help would be much appreciated. Thanks

AutoMapper is just a tool to map the properties of the View Model to/from your Domain Model.
The View Model is what you use in all of your Views, and your Domain Model is the underlying business model which shouldn't be exposed to the Views.
This is what AutoMapper simplifies, it maps properties of these two models so that we don't have to keep on converting one model to the other.
Now moving on to creating / updating related entities...
Say we want to add new Stock using the navigation property on the Item.
Item item = this.DbSource.Items.First(itemEntity => itemEntity.Id == 5);
if(item.Stocks == null) item.Stocks = new Collection<Stock>();
item.Stocks.Add(new Stock
{
StorageId = 3,
Amount = 123F
});
this.DbSource.SaveChanges();
Another case that you just pointed out was having a new Item and X amount of Stock of that Item, which you want to store in the database in a single operation.
Storage storage = this.DbSource.Storages.First(storageEntity => storageEntity.Id == 3);
if(storage.Stocks == null) storage.Stocks = new Collection<Stock>();
Stock stock = new Stock
{
StorageId = 3,
Amount = 123F,
Item = new Item
{
Name = "Redbull"
}
};
storage.Stocks.Add(stock);
this.DbSource.SaveChanges();
Or if you have no data in your database and you want all 3 models posted in a single go...
Stock stock = new Stock
{
Amount = 123F,
Item = new Item
{
Name = "Redbull"
}
};
Storage storage = new Storage
{
Name = "It's a secret"
};
storage.Stocks.Add(stock);
this.DbSource.Storages.Add(storage);
this.DbSource.SaveChanges();
Also modify all of your Models with a constructor which initializes a Collection on all of your ICollection navigational properties, this way you can avoid the NullReferenceException
So for example modify the Item class to this
public class Item
{
public Item()
{
this.Stocks = new Collection<Stock>();
}
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Stock> Stocks { get; set; }
}
Using the Item as root property
Collection<Stock> stocks = new Collection<Stock>();
Collection<Stock> stocks.Add(new Stock
{
StorageId = 123,
Amount = 1000F
});
Item item = new Item
{
Name = "Pizza",
Stocks = stocks
};
this.DbSource.SaveChanges();

Related

Entity Framework inserting new rows instead of updating them

I have a problem when I am updating data to database. When I want to update data, Entitiy Framework adds new rows to tables that can have multiple rows (tables that have foreign key).
Database model:
When I update Phone/Contact or Tags entity, Entity Framework automatically adds new row instead of updating it
Here is code that I used:
public string UpdateContact(Contact contact)
{
if (contact != null)
{
int id = Convert.ToInt32(contact.id);
Contact Updatecontact = db.Contacts.Where(a => a.id == id).FirstOrDefault();
Updatecontact.firstname = contact.firstname;
Updatecontact.lastname = contact.lastname;
Updatecontact.address = contact.address;
Updatecontact.bookmarked = contact.bookmarked;
Updatecontact.city = contact.city;
Updatecontact.notes = contact.notes;
Updatecontact.Emails1 = contact.Emails1;
Updatecontact.Phones1 = contact.Phones1;
Updatecontact.Tags1 = contact.Tags1;
db.SaveChanges();
return "Contact Updated";
}
else
{
return "Invalid Record";
}
}
EDIT:
Here is EF Model code:
Contact:
public partial class Contact
{
public Contact()
{
this.Emails1 = new HashSet<Email>();
this.Phones1 = new HashSet<Phone>();
this.Tags1 = new HashSet<Tag>();
}
public int id { get; set; }
public string firstname { get; set; }
public string lastname { get; set; }
public string address { get; set; }
public string city { get; set; }
public Nullable<byte> bookmarked { get; set; }
public string notes { get; set; }
public virtual ICollection<Email> Emails1 { get; set; }
public virtual ICollection<Phone> Phones1 { get; set; }
public virtual ICollection<Tag> Tags1 { get; set; }
}
Emails/Tags and Phone have same model (with different name for value)
public partial class Email
{
public int id { get; set; }
public int id_contact { get; set; }
public string email1 { get; set; }
public virtual Contact Contact1 { get; set; }
}
Update properties rather than set new objects.
Updatecontact.Emails1.email1 = contact.Emails1.email1;
Updatecontact.Phones1.number = contact.Phones1.number;
Updatecontact.Tags1.tag1 = contact.Tags1.tag1;
Edit: seems that your contact model has lists of emails, phones and tags. If this is so, then simple assignment won't work. Instead, when sent from the client, you have to find one-by-one and update:
foreach ( var email in contact.Emails1 )
{
// first make sure the object is retrieved from the database
var updateemail = Updatecontact.Emails1.FirstOrDefault( e => e.id == email.id );
// then update its properties
updateemail.email1 = email.email1;
}
// do the same for phones and tags
It's doing that because you're setting the different HashSet values to the values of a completely different collection, namely from what you call contact in that method. In order for you to properly do an update, you're going to have to loop through the emails, phones, and tags to check if those need to be added/updated/deleted on the actual object that you're trying to update.
First, why do you have to search for the contact if you are already receiving it by parameter? That makes me think that you are creating a new one because you are in a different context, if so, then it creates a new record because you have 2 different object in 2 different context.
Try using just one object in the same context to update, EF should mark the object to modification by itself, if not then try making sure before saving that your object has EntityState.Modified.

poco classes setting default value from another entity

public class Price
{
public int Id { get; set; }
public decimal Price { get; set; }
}
public class Product
{
public Product()
{
// Set default value here
}
public int Id { get; set; }
public string Sku { get; set; }
public int PriceId { get; private set; }
public virtual Price Price { get; set; }
}
I've searched in the internet the best way to set a default value in a field. They say put in the constructor or make a backing field. Now what if I want to set a default value in an entity from another entity's field value? Say, the default value of Product's Price is the latest Price(in the Price class)
How do you achieve that?
Price = Context.Price.FirstOrDefault().OrderByDescending(c => c.Id)?
Any help would be much appreciated. Thanks
What i would do is when i query the DB to return the Product / Products, i would let it fill the price for me.
This is a just for demonstration purposes
I would do something like:
public class MyDataAccessLayer
{
public IEnumerable<Product> GetProducts()
{
return DbContext.Products.Select(x => new Product
{
Price = Context.Price.FirstOrDefault().OrderByDescending(c => c.Id)
};
}
public Product GetProduct(string id)
{
var product = DbContext.Products.FirstOrDefault(x => x.Id == id);
if (product != null)
{
product.Price = Context.Price.FirstOrDefault().OrderByDescending(c => c.Id);
}
return product;
}
}
I would definitely let me Data Access Layer do the fetching for me and have it fill the latest price from the DB, and not have the POCO access my database.
Keep last inserted Producte in Cache. while you are creating new Product get last Product from Cache and set default values inside constructor and when you are inserting new Product update Cache.
While you are inserting new Product it's same as getting last Product from database because new inserted product is last product in database. So instead of:
var lastProduct = Context.Products.FirstOrDefault().OrderByDescending(c => c.Id);
do this:
var lastProduct = Context.Products.Add(newProduct);
Cache.Set("LastProduct", lastProduct, new CacheItemPolicy { SlidingExpiration = new TimeSpan(1, 0, 0, 0)});
There are some advantages here. first you dot query database every time for getting last product. second your are not going access EF Context inside your Poco class.
public class Product
{
public Product()
{
// var lastProduct = (Product)Cache["LastProduct"];
// Set default value here
}
public int Id { get; set; }
public string Sku { get; set; }
public int PriceId { get; private set; }
public virtual Price Price { get; set; }
}

Linq to Entities, Master Detail into DataContracts Query

This is part of my WebAPI, and I'm having trouble getting this data out of Linq to entities into my Datacontract objects. I will be returning a custom data type to the caller, whether they want it XML or JSON, I don't care, I'm just handing this to WebAPI to take care of it.
I'm using VS2013 and EF5, ASP.NET 4.5 in C#
The structure is as follows:
ProductCategory
ID
CategoryName
(a million other things)
List<Products>
Products
ID
ProductName
(a million other things)
Category
I have set up a DataContract that looks like the following:
ProductCategoryDataContract
ProductCategoryId
ProductCategoryName
List<ProductDataContract> Products
ProductDataContract
ProductName
Basically, I want to get a Linq query to return ALL categories, and within it ALL products.
from prodcat in context.ProductCategories order by prodcat.ItemOrder
select new ProductCategoryDataContract
{
ProductCategoryId = prodcat.Id
Products = prodcat.Products // this obviously fails as ProductDataContract != Products
}
If I try
Products = new List<ProductDataContract> { //initializer stuff }
I don't have any of the intellisense things I would think I would have (ProductName, etc), because I'm guessing I'm in the list.
Basically, I have all the relationships set up and I can get everything in straight up EF, but because I'm putting these into new datacontracts, it's giving me a little grief (mostly due to my lack of linq knowledge).
My question is:
1.) how can I do this
and
2.) how can I do this with minimal database hits. Potentially I'm firing off thousands of items within tens of product groups.
Thanks much, and if I'm not clear on anything, please lmk. And, the above is pseudocodish, not the real deal so if I made stupid naming errors, that's unlikely 'it' :)
public interface IProducts
{
int ProductId { get; set; }
decimal Price { get; set; }
List<IProductCategories> Categorieses { get; set; }
}
public interface IProductCategories
{
int ProductId { get; set; }
string ProductCategoryName { get; set; }
}
internal class Products : IProducts
{
public int ProductId { get; set; }
public decimal Price { get; set; }
public List<IProductCategories> Categorieses { get; set; }
}
internal class ProductCategories : IProductCategories
{
public int ProductId { get; set; }
public string ProductCategoryName { get; set; }
public ProductCategories(int productId, string productCategoryName)
{
ProductId = productId;
ProductCategoryName = productCategoryName;
}
}
public class ProductDataContract
{
public int ProductId { get; set; }
public List<IProductCategories> Categorieses { get; set; }
}
//Here is how you get your data:
// your retun objects
var products = new List<ProductDataContract>();
using (
var db =
new DataClassesDataContext(
ConfigurationManager.ConnectionStrings["TestConnectionString"].ConnectionString))
{
foreach (var prod in db.Products.Select(p => new Products {ProductId = p.ProductId}))
{
prod.Categorieses = new List<IProductCategories>();
foreach (var category in db.ProductCategories.Where(c => c.ProductId == prod.ProductId))
{
prod.Categorieses.Add(new ProductCategories(category.ProductId, category.ProductCategoryName));
}
products.Add(new ProductDataContract {Categorieses = prod.Categorieses, ProductId = prod.ProductId});
}
}

Recursive Entity Update

I have an entity which holds a list of entities (same as root entity) to represent a Folder structure:
public class SopFolder
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime? LastUpdated { get; set; }
public int Status { get; set; }
public virtual ICollection<SopField> SopFields { get; set; }
public virtual ICollection<SopFolder> SopFolderChildrens { get; set; }
public virtual ICollection<SopBlock> Blocks { get; set; }
public virtual ICollection<SopReview> Reviews { get; set; }
}
This entity is stored in my DB using Code-First Approach which is working fine. I then print the entity to a KendoUI Treeview, let the user modify it and on "save" post it back to the Server to an Action as IEnumerable<TreeViewItemModel> items.
I then look for the ROOT entity with all it's children (there is only one root) and convert it back into an SopFolder object.
To get the full object updated in the database I do the following:
List<SopFolder> sopfolderlist = ConvertTree(items.First());
SopFolder sopfolder = sopfolderlist[0];
if (ModelState.IsValid)
{
SopFolder startFolder = new SopFolder { Id = sopfolder.Id };
//db.SopFolders.Attach(startFolder);
// db.SopFolders.Attach(sopfolder);
startFolder.Name = sopfolder.Name;
startFolder.LastUpdated = sopfolder.LastUpdated;
startFolder.SopFields = sopfolder.SopFields;
startFolder.SopFolderChildrens = sopfolder.SopFolderChildrens;
startFolder.Status = sopfolder.Status;
db.Entry(startFolder).State = EntityState.Modified;
db.SaveChanges();
return Content("true");
}
However this is not working. The model is not updated at all. If I shift the "entityState.Modified" before the modifications, it just creates a complete fresh duplicate of my data in the database (modified of course).
Is my approach correct or do I have to go a different path? What am I missing here? I guess there is another "hidden" id which lets the EF map the entities to the db entries but I am not sure about this. Thanks for help!
UPDATE:
Instead of creatinga new instance of SopFolder I also tried db.SopFolders.Find(sopfolder.Id) and this works for entries with no children. If I have entities with children, it creates a duplicate.
Regards,
Marcus
This is typical Disconnected Graph scenario. Please see this question for possible solutions:
Disconnected Behavior of Entity Framework when Updating Object Graph
You have already figure out the first solution - that is: update entities separately. Actually, what you should do is to fetch the original data from database and then do comparison of what have changed. There are some generic ways of doing that, some of them are described in "Programming EF DbContext" book by J.Lerman, which I strongly recommend to you before doing more coding using EF.
P.S. IMHO this is the worse downside of EF.
Replace SopFolder startFolder = new SopFolder { Id = sopfolder.Id }; with
SopFolder startFolder = db.SopFolders.FirstOrDefault(s=>s.Id.Equals(sopfolder.Id));
// then validate if startFolder != null
I recommend you to create your entity model with ParentId, not children object list. When you need treeview model collect it with recursive function from database.
public class SopFolder
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime? LastUpdated { get; set; }
public int Status { get; set; }
public virtual ICollection<SopField> SopFields { get; set; }
//public virtual ICollection<SopFolder> SopFolderChildrens { get; set; }
public int? ParentFolderId { get; set; }
public virtual ICollection<SopBlock> Blocks { get; set; }
public virtual ICollection<SopReview> Reviews { get; set; }
}
When you create children folders, select it's parent, so collect your data. In childrens case try this :
List<SopFolder> sopfolderlist = ConvertTree(items.First());
SopFolder sopfolder = sopfolderlist[0];
if (ModelState.IsValid)
{
SopFolder startFolder = new SopFolder { Id = sopfolder.Id };
//db.SopFolders.Attach(startFolder);
// db.SopFolders.Attach(sopfolder);
startFolder.Name = sopfolder.Name;
startFolder.LastUpdated = sopfolder.LastUpdated;
startFolder.SopFields = sopfolder.SopFields;
startFolder.SopFolderChildrens = sopfolder.SopFolderChildrens;
foreach (var child in sopfolder.SopFolderChildrens)
{
db.SopFolders.CurrentValues.SetValues(child);
db.SaveChanges();
}
startFolder.Status = sopfolder.Status;
db.Entry(startFolder).State = EntityState.Modified;
db.SaveChanges();
return Content("true");
}

Retrieve navigation properties's data in a recently added record

I have 3 entities in my case. Invoice, InvoiceDetail and Item.
Invoice has a collection of InvoiceDetail.And each InvoiceDetail has an Item.
Please see the code below:
var ctx = new TestEntities();
var newInvoice = new Invoice
{
CreationDate = DateTime.Now,
UserId = 14
};
newInvoice.InvoiceDetails.Add(new InvoiceDetail
{
ItemId = 345,
ItemCount = 10
});
newInvoice.InvoiceDetails.Add(new InvoiceDetail
{
ItemId = 534,
ItemCount = 10
});
ctx.Invoices.Add(newInvoice);
ctx.SaveChanges();
// workaround
// ctx.Items.ToList();
foreach (var i in newInvoice.InvoiceDetails)
{
// In this line I get NullReferenceException
Console.WriteLine(i.Item.Title);
}
I get NullReferenceException when I want to retrieve each InvoiceDetail's Item data.
The problem is solved when I uncomment, commented part of the code. (ctx.Items.ToList())
UPDATE 1 :
And also this is the Item class:
public partial class Item
{
public Item()
{
this.InvoiceDetails = new HashSet<InvoiceDetail>();
}
public long Id { get; set; }
public string Title { get; set; }
public virtual ICollection<InvoiceDetail> InvoiceDetails { get; set; }
}
UPDATE 2:
public partial class InvoiceDetail
{
public long Id { get; set; }
public long InvoiceId { get; set; }
public long ItemId { get; set; }
public int ItemCount { get; set; }
public virtual Invoice Invoice { get; set; }
public virtual Item Item { get; set; }
}
[NOTE: I am assuming EF5]
The problem could be related to the way you create instances of Invoice and InvoiceDetail. You are newing up instances so they are not EF proxies with all of the necessary components for lazy loading.
I suggest you try using the DbSet.Create() method instead of new
var newInvoice = ctx.Set<Invoice>().Create();
newInvoice.CreationDate = DateTime.Now;
newInvoice.UserId = 14;
var detail1 = ctx.Set<InvoiceDetail>().Create();
detail1.ItemId = 345;
detail1.ItemCount = 10;
newInvoice.InvoiceDetails.Add(detail1);
//...
I can't promise this will fix your problem as EF is such an intricate and varied beast but it is worth giving this a try ...

Categories

Resources