entity-framework-4.1

Turn off EF change tracking for any instance of the context [duplicate]

北慕城南 提交于 2019-12-28 06:28:16
问题 This question already has answers here : Global setting for AsNoTracking()? (5 answers) Closed 6 years ago . I have a context to a read-only database for reporting and I am writing lots of code, like this: using (var context = new ReportingContext()) { var reportXQuery = context.ReportX.AsNoTracking(); // Do stuff here with query... } Is there a way to set the AsNoTracking bit so that just new ing up the ReportingContext above would automatically use AsNoTracking instead of needing to

EF Code First Additional column in join table for ordering purposes

你离开我真会死。 提交于 2019-12-28 05:33:33
问题 I have two entities that have a relationship for which I create a join table public class Student { public int Id { get; set; } public string Name { get; set; } public virtual ICollection<Image> Images { get; set; } } public class Image { public int Id { get; set; } public string Filename { get; set; } public virtual ICollection<Student> Students { get; set; } } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Student>() .HasMany(i => i.Images)

Navigation Property without Declaring Foreign Key

霸气de小男生 提交于 2019-12-28 01:55:28
问题 All my models contain at least two associations. When modeling this in ef4 I've only been able to do this without a second Foreign Key property through the use of the fluent interface. ForeignKey seems like the right attribute to use, except for the fact that it requires a string parameter. So my question is, can you have a navigational property and declare it as such using an attribute? public class User : IAuditable { // other code public virtual User Creator { get; set; } public virtual

Entity framework code-first null foreign key

人盡茶涼 提交于 2019-12-27 17:10:06
问题 I have a User < Country model. A user belongs to a country, but may not belong to any (null foreign key). How do I set this up? When I try to insert a user with a null country, it tells me that it cannot be null. The model is as follows: public class User{ public int CountryId { get; set; } public Country Country { get; set; } } public class Country{ public List<User> Users {get; set;} public int CountryId {get; set;} } Error: A foreign key value cannot be inserted because a corresponding

ADO.NET DbContext Generator vs. ADO.NET Poco Entity Generator (ObjectContext)

☆樱花仙子☆ 提交于 2019-12-27 11:54:39
问题 I am about to start implementing the data access infrastructure of a project that was architected with an approach to DDD ( it's my first attempt on DDD, so be gentle ;-) ). I will be using Entity Framework. Until now, I was looking into the method teached by Julie Lerman on her great book, Programming Entity Framework, where ADO.NET POCO Entity Generator is used, with some changes to the T4 templates and some more custom code. Today I started reading articles on EF4.1 and the ADO.NET

ADO.NET DbContext Generator vs. ADO.NET Poco Entity Generator (ObjectContext)

风流意气都作罢 提交于 2019-12-27 11:54:18
问题 I am about to start implementing the data access infrastructure of a project that was architected with an approach to DDD ( it's my first attempt on DDD, so be gentle ;-) ). I will be using Entity Framework. Until now, I was looking into the method teached by Julie Lerman on her great book, Programming Entity Framework, where ADO.NET POCO Entity Generator is used, with some changes to the T4 templates and some more custom code. Today I started reading articles on EF4.1 and the ADO.NET

Better way to query a page of data and get total count in entity framework 4.1?

折月煮酒 提交于 2019-12-27 11:54:13
问题 Currently when I need to run a query that will be used w/ paging I do it something like this: //Setup query (Typically much more complex) var q = ctx.People.Where(p=>p.Name.StartsWith("A")); //Get total result count prior to sorting int total = q.Count(); //Apply sort to query q = q.OrderBy(p => p.Name); q.Select(p => new PersonResult { Name = p.Name }.Skip(skipRows).Take(pageSize).ToArray(); This works, but I wondered if it is possible to improve this to be more efficient while still using

How call functions from data base in EF

痴心易碎 提交于 2019-12-25 08:27:16
问题 I generate a model from my database. In the .edmx file I have a row string <Function Name="GetUniqueInt" ReturnType="int" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="true" ParameterTypeSemantics="AllowImplicitConversion" Schema="dbo" /> as its cause? 回答1: You need to create stub method for your function somewhere. It should look like: [EdmFunction("YourModelNamespace", "GetUniqueInt")] public static int GetUniqueInt() { throw new NotSupportedException("Direct calls

Why can I not use the same poco in code first in EF4.1 to project my data?

旧街凉风 提交于 2019-12-25 05:27:21
问题 Consider the following senario: I have a code first model in my project with the Poco object Animal. The animal row has 50 properties and I only want 5 of them in my application. So I will try to project the 5 properties to a Poco object like this List<Animal> animals = (from an in dbContext.Animal where an.IsMammal select new Animal { Id = an.Id , Color = an.Color , Height = an.Height, Width = an.Width , Hair = an.Hair }).ToList(); Is not working for Animal is a StrongTyped object and cannot

DbContext declaration - Framework 4.1 - MVC 3.0

别说谁变了你拦得住时间么 提交于 2019-12-25 04:06:59
问题 Is it correct to declare a global variable of "DBContext" in a controller and then use it for all database operations? Example: public class ProductController : Controller { private readonly DBContextEntities _db = new DBContextEntities(); public ActionResult Index() { var products = _db.Products.ToList(); return View(products); } public ActionResult Create() { _db.Products.AddObject(new Product{Name="x",Price="5.2"}); _db.SaveChanges(); return View(products); } } Please Advice, 回答1: I have