entity-framework-4.1

Update a single property of a record in Entity Framework Code First

妖精的绣舞 提交于 2019-11-30 06:37:50
How can I update a single property of a record without retrieving it first? I'm asking in the context of EF Code First 4.1 Says I have a class User, mapping to table Users in Database: class User { public int Id {get;set;} [Required] public string Name {get;set;} public DateTime LastActivity {get;set;} ... } Now I want to update LastActivity of a user. I have user id. I can easily do so by querying the user record, set new value to LastActivity, then call SaveChanges(). But this would result in a redundant query. I work around by using Attach method. But because EF throws a validation

EF ICollection Vs List Vs IEnumerable Vs IQueryable

↘锁芯ラ 提交于 2019-11-30 06:11:32
问题 so, my EF model has relationships and according to what I have seen in examples, those relationships should be done with virtual properties of ICollection. Example: public class Task { public int Id { get; set; } public string Description { get; set; } public virtual ICollection<SubTask> { get; set; } } I read somewhere that I should use IEnumerable to prevent deferred execution, is that correct? It means that if my DAL methods return IEnumerable, still of IQueryable, the SQL will be executed

Convert from to string in database to boolean property Entity Framework 4.1

你离开我真会死。 提交于 2019-11-30 06:01:29
问题 i am working on a legacy oracle database that uses character literals T and F in the database for its boolean values however i would like the Entity property to reflect a proper boolen value is there a wy to convert this value when the model is binding it is a read only database so inserts are not important 回答1: I would suggest wrapping up or extending the generating type to add this sort of functionality ... entity framework will generate objects that basically match what the data in the

Multiple Primary Key with asp .net mvc 3

 ̄綄美尐妖づ 提交于 2019-11-30 05:35:14
I'm using asp .net mvc 3, and I have a problem with an entity that contains 2 primary key, when I try to insert data in the table. public class LineItem { [Key] public int OrderId { get; set;} [Key] public int LineNum { get; set;} public string ItemId { get; set;} public int Quantity { get; set;} public decimal UnitPrice { get; set; } } when I try to insert I got this error : Unable to determine composite primary key ordering for type 'ApplicationMVC3.Models.LineItem'. Use the ColumnAttribute or the HasKey method to specify an order for composite primary keys. May someone help me please !!

How to get the Primary Key(s) in Entity Framework 4.1, i.e. using DbContext

╄→гoц情女王★ 提交于 2019-11-30 04:58:56
The answers I'm seeing here are for ObjectContext. Is there a property to determine an entity's primary key names when using DbContext? Ah.. one of those times that I wish Entity Framework is open source! I can glean this primary key name information from .Find method :-) Ladislav Mrnka You cannot use DbContext for that - DbContext API is just dumb wrapper with only most needed functionality. For everything more complex you must convert DbContext back to ObjectContext and use it. Try something like this: Extract key names : public static string[] GetEntityKeyNames<TEntity>(this DbContext

unidirectional many-to-many relationship with Code First Entity Framework

混江龙づ霸主 提交于 2019-11-30 04:55:31
I am new to EF, and trying to get many-to-many unidirectional relationship with code first approach. For example, if I have following two classes (not my real model) with be a N * N relationship between them, but no navigation property from "Customer" side. public class User { public int UserId { get; set; } public string Email { get; set; } public ICollection TaggedCustomers { get; set; } } public class Customer { public int CustomerId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } } The mapping code looks like ... modelBuilder.Entity() .HasMany(r =>

Code First vs. Database First

强颜欢笑 提交于 2019-11-30 04:46:44
I created an Entity Framework model based on an existing database, then generated the POCO entities from the model. The connection string in my web.config isn't Entity Framework, it's just the standard connection string (it's missing the CSDL, SSDL, MSL references). I can compile my application, but when I run I get this error: Code generated using the T4 templates for Database First and Model First development may not work correctly if used in Code First mode. To continue using Database First or Model First ensure that the Entity Framework connection string is specified in the config file of

Entity Framework Oracle and Sql Server - how to build a database independent application

て烟熏妆下的殇ゞ 提交于 2019-11-30 03:45:49
问题 We are trying to build a data access layer for using both Oracle and SQL Server (not at the same time). We use EF Model-first for creating the model and the create the SQL scripts for building the database. Our first thought was to create 2 EDMX files, one for each type, and use the appropriate one depending on the client's need. We are using the Oracle and SQL Server database generation workflow and DDL generation template to create the scripts for each database. Our main problem is when the

System.OutOfMemoryException using Entity Framework?

拥有回忆 提交于 2019-11-30 03:18:51
问题 I am trying to save hundreds of thousands of records using Entity framework. After saving few hundreds of thousands of records I get following error: :System.OutOfMemoryException My code foreach (BibContent objbibcontents in lstBibContent) { db.BibContents.AddObject(objbibcontents); c = c + 1; if (c == 1000) { db.SaveChanges(); c = 0; } } I noticed after saving 1000 records my db is not overriding another 1000 records. it is adding them into my dbcontext. I am creating a new instance after

Wait! which config file? (Entity Framework Connection String)

╄→尐↘猪︶ㄣ 提交于 2019-11-30 03:16:43
So, I created my entity model in a separate class library. I had to add the connection string to the app.config file of that class library. Then I added a ref for that project in my web application. I added the same connection string in the web.config of my web application thinking this is where Entity Framework will be reading the connection string from. Everything was fine up until I deployed my web app. When I deployed I change the connection string in the web.config (not app.config of the class library) and I started getting errors. After doing some research I found out that the connection