entity-framework-5

Best Way to display a user friendly error message for the users, when an DBupdateexception occurred

為{幸葍}努か 提交于 2019-12-13 03:54:02
问题 I have the following code inside my action method : Try { repository.Save(); DetailBox b = new DetailBox() { Tag = repository.getTechnologyTagByIT360ID(resourceid) }; return PartialView("_detailBox", b); } catch (DbUpdateException e) { ModelState.AddModelError(string.Empty, "Error occurred:" + e.InnerException.InnerException.Message); } Currently, if the DbUpdatException occurs it will show the following error to the user: • Error occurred: Violation of UNIQUE KEY constraint 'IX_Technology_2'

DbContext get IQueryable for scalar system functions (Count, Any, Sum, Max)

久未见 提交于 2019-12-13 02:23:57
问题 I have DBContext with DbSet called Assignments. It's not a problem to create queryable for enumerable expressions and concatenated them, however I don't see the way to get IQueryable with deferred execution for functions like Count, Any, Max, Sum. Basically I want to have some IQueryable extension so I can execute it like this: IQueryable<int> query = myDbContext.SelectValue((ctx)=>ctx.Assignments.Where(...).Count()) .UnionAll(myDbContext.SelectValue((ctx)=>ctx.Assignments.Where(...).Count())

How to map a stored procedure result to a custom class?

纵饮孤独 提交于 2019-12-13 02:10:02
问题 I am using entity framework 5 and I've added two stored procedures to my .edmx model. The first stored procedure returns a string and if I open the model browser in Visual Studio, I can change the Returns a Collection Of section and set the scalar type to String, as follows: Then in the code, I can call the SP and get the result like follows: using (var context = new MyEntities()) { var spResult = context.mySPNameFromTheModel(exampleParameter1, exampleParameter2); // This will get the output

LINQ to EF Working with Lookup Tables

坚强是说给别人听的谎言 提交于 2019-12-13 01:35:09
问题 I have a main VendorProfile table and a 1-many VendorHistory table that contains status codes and date stamps. The query below works at retrieving only the latest status (status code and date) for each vendor. However, what I also want to do is translate the status code into the status name, which is located in StatusCodes lookup table. Model Diagram public IEnumerable<BrowseStatusModel> BrowseByStatus() { IQueryable<BrowseStatusModel> viewModel = _db.VendorProfiles .Include(

Breeze does not expand a navigation property

依然范特西╮ 提交于 2019-12-13 00:56:55
问题 I have a Breeze dataservice (aka datacontext) in my Single Page Application. I want to get a list of Runs from a WebAPI controller along with a list of OutlineItems for each run. The controller is returning the list of open Runs with child OutlineItems with this method on the BreezeController. [AcceptVerbs("GET")] public IQueryable<Run> Runs() { return _contextProvider.Context.Runs .Include("RunOutlineItems") .AsQueryable() .Where(r => r.RunStatusId < 4); // 4 is the cutoff for open items }

When exactly does Entity Framework Fire a SQL Command?

丶灬走出姿态 提交于 2019-12-13 00:49:21
问题 Suppose I have code like: public void TestWhenSqlFires() { var db = new Db(); // EF DbContext var items = db.SimpleObjects; // SimpleObject implements interface IId var byId = WhereId(items, 1); var array = byId.ToArray(); } protected IEnumerable<IId> WhereId(IEnumerable<IId> items, int id) { return items.Where(i => i.Id == id); } At what line in TestWhenSqlFires() will SQL actually be run against the database? (This is a question that spun off from comments on this answer) 回答1: One way to

Using Entity Data Model in VS 2012

[亡魂溺海] 提交于 2019-12-13 00:40:50
问题 I have used Entity Data Model before with VS 2010, but now I am having trouble with VS 2012. First of all, now in 2012 Entity Data Model there are two new files with .tt extension. Also the Designer.cs file is empty and has a message how to enable the code generation, but when I do enable the code generation it says that the objects already exist. I am also missing the CreateObjectSet(); using (MyEntities entitiesContext = new MyEntities()) { var entitySet = entitiesContext.CreateObjectSet<T>

Many to many relationship update: Cannot insert duplicate key

≡放荡痞女 提交于 2019-12-12 19:55:47
问题 I am still new in EF code first, so please be lenient with me. I have these entity classes: public class User { public int UserId { get; set; } public string UserName { get; set; } public string EmailAddress { get; set; } public string Password { get; set; } public virtual ICollection<Task> Tasks { get; set; } public virtual ICollection<Task> TaskAssignees { get; set; } public User() { Tasks = new List<Task>(); } } public class Task { public int TaskId { get; set; } public string Name { get;

What is the difference between EntityTypeConfiguration and DbMigration for a new EF project

柔情痞子 提交于 2019-12-12 17:06:33
问题 Should I use EntityTypeConfiguration to build my data model, or should I directly enable migrations an use DbMigration . That is use : public class BlogEFCFConfiguration : EntityTypeConfiguration<Blog> { public BlogEFCFConfiguration() : base() { HasKey(x => x.BlogId); Property(x => x.BlogId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); Property(x => x.Name).HasColumnType("varchar").HasMaxLength(128); } } or public partial class InitialCreate : DbMigration { public override

How to avoid N+1 in EF generated queries

故事扮演 提交于 2019-12-12 16:24:10
问题 I used to generate all the helper tables like UsergroupUsers for many-to-many relations or relational Ids in POCO myself but now I want EF to take care of them. Now I don't think it's such a good idea after all. Problem When I try to get all UsergroupDynamicField for particular user it generates N+1 query for every usergroup user is in. Here I overcommed this problem by simply stating that Usergroups will be IQUeriable instead of IEnumerable . Now I cannot do that because EF won't map it, it