dbcontext

Entity Framework (4.2) HasRequired results in unexpected LEFT OUTER JOIN

浪子不回头ぞ 提交于 2019-12-01 03:32:35
It appears that the Entity Framework (latest version from NuGet) may be ignoring the HasRequired configuration when constructing joins for Navigation Properties other than the first one defined. For example, given a POCO object (Person) with the following configuration: var person = modelBuilder.Entity<Person>(); person.ToTable("The_Peoples"); person.HasKey(i => i.Id); person.Property(i => i.Id).HasColumnName("the_people_id"); person.HasRequired(i => i.Address) .WithMany() .Map(map => map.MapKey("address_id")); person.HasRequired(i => i.WorkPlace) .WithMany() .Map(map => map.MapKey("work_place

Entity Framework 4.1 Database First does not add a primary key to the DbContext T4 generated class

倾然丶 夕夏残阳落幕 提交于 2019-12-01 01:25:56
问题 I am just getting started with Entity Framework 4.1, trying out the "database first" mode. When EF generates a Model class with the "ADO.Net DbContext Generator," shouldn't it identify the primary key for the class with a [Key] attribute? Without this, it appears incompatible with the T4 MVCScaffolding. Here are the details: Using the Entity Data Model Designer GUI, I have added a simple "country" table to the model from my existing database. The GUI correctly identifies a single integer

Ninject scoping for DBContext used in Quartz.Net job

萝らか妹 提交于 2019-11-30 21:15:31
What's the best scoping to use for a DbContext implementation that gets instantiated via Ninject dependency resolver during execution of a Quartz.Net job implementation? If I used thread scope, will the same instance of DbContext get served if the same thread in Quartz's thread pool is used to execute the job multiple times? I would like a scope that means I get one (and only one) new instance of the DbContext each time the job is fired. BatteryBackupUnit Yes, i would advise against using InThreadScope . When a thread-pool thread is used, there will be leakage. Furthermore, there's nothing

EF DbContext. How to avoid caching?

ぐ巨炮叔叔 提交于 2019-11-30 17:43:18
问题 Spent a lot of time, but still cann't understand how to avoid caching in DbContext. I attached below entity model of some easy case to demonstrate what I mean. The problem is that dbcontext caching results. For example, I have next code for querying data from my database: using (TestContext ctx = new TestContext()) { var res = (from b in ctx.Buildings.Where(x => x.ID == 1) select new { b, flats = from f in b.Flats select new { f, people = from p in f.People where p.Archived == false select p

Using TransactionScope with Entity Framework 6

非 Y 不嫁゛ 提交于 2019-11-30 11:57:49
问题 What I can't understand is if its possible to make changes to the context and get the changes in the same transaction before its commited. This is what I´m looking for: using (var scope = new TransactionScope(TransactionScopeOption.Required)) { using (var context = new DbContext()) { //first I want to update an item in the context, not to the db Item thisItem = context.Items.First(); thisItem.Name = "Update name"; context.SaveChanges(); //Save change to this context //then I want to do a

Multithreading Entity Framework: The connection was not closed. The connection's current state is connecting

非 Y 不嫁゛ 提交于 2019-11-30 11:54:33
问题 So I have a windows service process that performs a workflow process. The back end uses Repository and UnitofWork Pattern and Unity on top of Entity Framework with the entities class generated from the edmx. I won't go into a whole lot of detail as its not necessary but basically there are 5 steps that the workflow goes through. A particular process might be at any stage at any point in time (in order of course). Step one just generates data for step two, which validates the data via a long

EF and repository pattern - ending up with multiple DbContexts in one controller - any issues (performance, data integrity)?

我是研究僧i 提交于 2019-11-30 11:39:17
问题 Most of my knowledge of ASP.NET MVC 3 comes from reading through the book Pro ASP.NET MVC 3 Framework by Adam Freeman and Steven Senderson. For my test application I have tried to stick to their examples very closely. I am using the repository pattern plus Ninject and Moq which means that unit testing work quite well (i.e. without needing to pull data from the database). In the book repositories are used like this: public class EFDbTestChildRepository { private EFDbContext context = new

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

混江龙づ霸主 提交于 2019-11-30 10:15:09
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? I have found an answer that works for me. I declare my DbSet properties as my derived interface in my context, e.g.: IDerivedDbSet<Customer> Customers {

How to return dynamic object from SQL query

余生颓废 提交于 2019-11-30 09:41:34
问题 I have situation where a storeprocdure return collection, but I do not how the object structure because the query is very dynamic. One query can return: Id | Location | MarketSegment | ... n columns and another can return Id | Sales Rep | Location | Region | ... n columns I am simply just return a "object" as you can see in the code below. I know this won't work, but how can I set it up so it does? using (DbContext db = new Context()) { var items = db.Database.SqlQuery<object>( "SP @Param1,

Entity Framework 4.2 : Getting proper database errors

故事扮演 提交于 2019-11-30 09:38:50
问题 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 {