dbcontext

Entity Framework 4.2 : Getting proper database errors

不打扰是莪最后的温柔 提交于 2019-11-29 15:47:29
In my ASP.NET MVC 3 application, I use EF 4.2. In my database, I have a unique constraint for a column. I try to insert same data in order to see what I get but I am getting the below error: An error occurred while updating the entries. See the inner exception for details. Inside the inner exception I can see the full error about unique constraint. But how can I uniquely catch this exception to tell the user this: You are entering the same value again. Here is what I do currently: try { UpdateModel<ConditionType>(conditionType, null, null, new string[] { "ConditionTypeId" });

Inherits from DbSet<T> with the purposes to add property

对着背影说爱祢 提交于 2019-11-29 15:22:03
问题 Is there a way to inherits from DbSet? I want to add some new properties, like this: public class PersonSet : DbSet<Person> { public int MyProperty { get; set; } } But I don't know how to instantiate it in my DbContext public partial MyContext : DbContext { private PersonSet _personSet; public PersonSet PersonSet { get { _personSet = Set<Person>(); // Cast Error here _personSet.MyProperty = 10; return _personSet; } } } How can I achieve this? 回答1: I have found an answer that works for me. I

DbContext SaveChanges Order of Statement Execution

…衆ロ難τιáo~ 提交于 2019-11-29 13:16:53
I have a table that has a unique index on a table with an Ordinal column. So for example the table will have the following columns: TableId, ID1, ID2, Ordinal The unique index is across the columns ID1, ID2, Ordinal. The problem I have is that when deleting a record from the database I then resequence the ordinals so that they are sequential again. My delete function will look like this: public void Delete(int id) { var tableObject = Context.TableObject.Find(id); Context.TableObject.Remove(tableObject); ResequenceOrdinalsAfterDelete(tableObject); } The issue is that when I call Context

MVC, DbContext and Multithreading

社会主义新天地 提交于 2019-11-29 11:41:16
There are lots of questions about these subjects separately and everyone have their own opinion. Maybe someone can give me a good answer regarding the following issue. I have an Asp.NET MVC web service which uses EntityFramework for accessing the DB. There's a single controller and an instance of it is created each time a user makes a request to the web service. Every request is fast. It just gets some data from DB, changes it and then saves it. The question of course is how to maintain the DbContext (since it's not thread safe) ? On the ctor of the controller i create an instance of DbContext

C# EF 4.1 Create table dynamically within DbContext

一笑奈何 提交于 2019-11-29 11:17:43
I want to add tables to a SQLCe database at runtime, since the tablenames are not static and known at compile time. I try to do this with Entity Framework 4.1 and DbContext as follows: public class PersonContext : DbContext { public PersonContext() : base("UnicornsCEDatabase") { } } public class Person { public int NameId { get; set; } public string Name { get; set; } } public class Program { static void Main(string[] args) { using (var db = new PersonContext()) { db.Database.Delete(); //Try to create table DbSet per = db.Set<Person>(); var per1 = new Person { NameId = 1, Name = "James"}; per

Oracle ODP.Net and EF CodeFirst - edm.decimal error

雨燕双飞 提交于 2019-11-29 11:15:59
I have the following simple entity: public class Something{ [DatabaseGenerated(DatabaseGeneratedOption.Computed)] public int ID { get; set; } public string NAME { get; set; } public int STATUS { get; set; } } As you can see, I do not want the ID is generated from the database but I'm going to enter manually. This my DbContext class: public class MyCEContext : DbContext { ... public DbSet<Something> Somethings { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { string dbsch = "myce"; modelBuilder.Entity<Something>().ToTable("SOMETHING", dbsch); } } There is

How do I stop Entity Framework from generating BOTH ObjectContext and dbContext

限于喜欢 提交于 2019-11-29 07:26:12
问题 Background Visual Studio 2012 NEW Model.EDMX file created in VS2012 Copied some of the EDMX xml from a previously created EDMX into the new one Problem / Question Now. The EDMX (TT transform, custom tool, whatever, etc.) is generating BOTH DbContext classes (under the Model.tt/Model.Context.tt files) and ObjectContext classes (via the Model.designer.cs file). See image below: Everything builds fine and works with the DbContext (but obviously only if I delete the Designer.cs file just before

Is it always better to use 'DbContext' instead of 'ObjectContext'?

不打扰是莪最后的温柔 提交于 2019-11-29 06:23:30
I just downloaded EntityFramework.dll v4.3 . I've found a number of questions that compare DbContext vs. ObjectContext . But most of these are from 2010, or early 2011. I'd like to read more on the subject. Specifically, are there any books on DbContext I can get my hands on? I also want to know, as of today, what are the limitations of DbContext when comparing it to its older brother the ObjectContext ? I realize that DbContext is more compact in that it exposes fewer properties. This suggests to me that I should migrate from ObjectContext . But, if I do this migration, will I give up any

Entity Framework relationships between different DbContext and different schemas

淺唱寂寞╮ 提交于 2019-11-29 05:44:05
So, I have two main objects, Member and Guild. One Member can own a Guild and one Guild can have multiple Members. I have the Members class in a separate DbContext and separate class library. I plan to reuse this class library in multiple projects and to help differentiate, I set the database schema to be "acc". I have tested this library extensively and can add, delete, and update Members in the acc.Members table. The Guild class is as such: public class Guild { public Guild() { Members = new List<Member>(); } public int ID { get; set; } public int MemberID { get; set; } public virtual Member

How to configure DbContext to work with Oracle ODP.Net and EF CodeFirst?

泪湿孤枕 提交于 2019-11-29 02:32:34
I'm trying to work with EF CodeFirst under Oracle with ODP.net. This is my DbContext class: public class MyCEContext : DbContext { public DbSet<Person> Persons { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Person>().ToTable("PERSONS","myce"); } public MyCEContext() : base(new OracleConnection( "Data Source=cebd; User ID=myce; Password=****;"), true) {} } Problem is that when I try to do something like this: MyCEContext context = new MyCEContext(); Person p = context.Persons.Find(1); I get this inner error: {"ORA-00942: table or view