dbcontext

EF5 db.Database.SqlQuery mapping returned objects

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 11:14:17
I have two C# classes public class SearchResult { public int? EntityId { get; set; } public string Name { get; set; } public Address RegisteredAddress { get; set; } } and public class Address { public int? AddressId { get; set; } public string Address1 { get; set; } public string Address2 { get; set; } public string Address3 { get; set; } } this is used in a dbContext call to map out the returning objects from a database via EF5 using (DbEntities db = new DbEntities()) { querySearchResult = db.Database.SqlQuery<SearchResult>( @"SELECT e.entity_id AS EntityId, e.entity_reg_name AS Name, a

Entity Framework - OnModelCreating with Model First

主宰稳场 提交于 2019-11-28 10:47:09
问题 I have a Entity Framework model set up with text templates to generate the code. However, one of them creates the DBContext containing an OnModelCreating . protected override void OnModelCreating(DbModelBuilder modelBuilder) { throw new UnintentionalCodeFirstException(); } How do expand on this, since a partial class with the same override does not work. 回答1: You cannot use OnModelCreating when using model first (EDMX). OnModelCreating is only for scenarios without using EDMX. 回答2: Possibly

EF 4.1 OnModelCreating not called

余生长醉 提交于 2019-11-28 09:06:27
I have a problem with EF 4.1 not calling OnModelCreating so that I can configure tables etc. I have an existing database. Here is my connection string: <add name="AcmeDBContext" connectionString="metadata=res://*/|res://*/|res://*/; provider=System.Data.SqlClient; provider connection string=" data source=[server]; initial catalog=Acme;integrated security=True; multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" /> Here is my class inherited from DbContext : public class AcmeDBContext : DbContext { public AcmeDBContext() : base() { Database.SetInitializer

DbContext SaveChanges Order of Statement Execution

ε祈祈猫儿з 提交于 2019-11-28 07:14:09
问题 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

Why must I have a parameterless constructor for Code First / Entity Framework

ε祈祈猫儿з 提交于 2019-11-28 07:03:00
问题 This is more of a question of "Why we do things" as my actual problem was solved but I don't know why. I was dealing with the following code inside my CountyRepository: public IEnumerable<County> GetCounties(string stateAbbr) { using (var db = new AppDbContext()) { State state = (from s in db.States where s.Abbr == stateAbbr select s).First(); return context.Counties.Where(c => c.StateID == state.StateID).ToList(); } } The AppDbContext I created above would go to a custom Initializer: public

Using Automapper to update an existing Entity POCO

陌路散爱 提交于 2019-11-28 06:55:20
I am using EF4 DbContext to provide the model for an ASP.NET MVC app. I use ViewModels to provide data to the views and Automapper to perform the mapping between the EF POCOs and the ViewModels. Automapper does a great job but I'm not clear the best way to use it after the ViewModel is posted back to the controller to carry out an update. My idea is to get POCO object using a key contained in the ViewModel. I then want to use Automapper to update the POCO with data from the ViewModel: [HttpPost] public ActionResult Edit(PatientView viewModel) { Patient patient = db.Patients.Find(viewModel.Id);

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

拜拜、爱过 提交于 2019-11-28 04:50:22
问题 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) {

How to set CommandTimeout for DbContext?

佐手、 提交于 2019-11-28 04:49:41
I am looking a way to set CommandTimeout for DbContext. After searching I found the way by casting DbContext into ObjectContext and setting value for CommandTimeout property of objectContext. var objectContext = (this.DbContext as IObjectContextAdapter).ObjectContext; But I have to work with DbContext. It will work with your method. Or subclass it (from msdn forum ) public class YourContext : DbContext { public YourContext() : base("YourConnectionString") { // Get the ObjectContext related to this DbContext var objectContext = (this as IObjectContextAdapter).ObjectContext; // Sets the command

MVC, DbContext and Multithreading

点点圈 提交于 2019-11-28 04:33:59
问题 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 is very slow when adding and deleting

[亡魂溺海] 提交于 2019-11-28 04:19:14
When using DbContext in a database-first scenario I found out that adding and deleting entities is very slow compared to ObjectContext. If adding 2000 entities and saving the changes at the end, DbContext is 3 to 5 times slower than ObjectContext (btw.: I know that adding a large amount of entities would be better using SqlBulkCopy but that's not the point). If saving changes after each addition DbContext is still nearly two times slower. When it comes to deletion it even gets worse: When saving at the end of all entity removals, DbContext is around 18 times slower than ObjectContext. I took