entity-framework-4

How to use parameters in Entity Framework in a “in” clause?

佐手、 提交于 2019-12-12 10:51:30
问题 I am using Entity Framework 4.0 and I want to use the following query: To do that I do the following: strSQLQuery = "select * from MyTable where IDData IN (@IDs)"; lstParameters.Clear(); myParameter = new SqlParameter("@IDs", strIDs); lstParameters.Add(myParameter); myContext.MyTable.SqlQuery(strSQLQuery, lstParameters.ToArray<object>()).ToList<MyTable>(); But I get an exception that say that it is not possible to convert nvarchar to bigint . That is because the parameter is the type string,

Context does not contain a definition for ExecuteStoreCommand

会有一股神秘感。 提交于 2019-12-12 10:20:15
问题 I'm trying to execute a query against my data model with context.ExecuteStoreCommand but I keep getting the error message: <object context> does not contain a definition for 'ExecuteStoreCommand' ... I've used this before in an old project and I can't see what I'm missing. I've got project references to EntityFramework, System.Data, System.Data.Entity. The MSDN documentation says that ObjectContext is part of System.Data.Object but I can't reference that directly and I've not needed to

Entity Framework 4 - How to cap the number of child elements from another table (connected via a foreign key)

我怕爱的太早我们不能终老 提交于 2019-12-12 09:53:13
问题 I have two data models Blog and Post . BlogId is a foreign key on the Post table public class Blog { public int ID { get; set; } public string Title { get; set; } public virtual ICollection<Post> Posts { get; set; } ... } public class Post { public int ID { get; set; } public virtual int BlogId { get; set; } public string Title { get; set; } ... } Now this works fine and my Repository is happy and pulls everything as expected from DB. My question is - Is there a way to limit the number of

How do I represent an option calculated column with EF Code First?

天大地大妈咪最大 提交于 2019-12-12 09:52:54
问题 I have a situation where I have a table of entries that I'll be querying on, but in some cases I'll have additional information available. For instance, if I have a table of people , I want to be able to search by name, but I also want to store some coordinates and search based on their location, but also expose that distance in the object model. So, for instance, suppose my people table looks something like this: PersonId int Name nvarchar(100) Address nvarchar(100) Latitude float(10,6)

Entity Framework - how to raise OnChanging for any property?

我是研究僧i 提交于 2019-12-12 09:22:09
问题 In a WPF / EF4.0 / MVVM app I have a View that edits a Customer entity. What is the best way to set a property "bool IsCustomerInEditMode" in my CustomerViewModel short of acting upon OnChanging/OnChanged partial methods for every single individual property of the Entity? As far as I know there's no OnEntityChanging method... Thanks! Edit: Answer: EntityState 回答1: Edit: To answer your question in your post (best way to set bool IsCustomerInEditMode) - Subscribe to the entity.PropertyChanging

How to bind gridview using linq/Entity Framework?

心不动则不痛 提交于 2019-12-12 08:44:24
问题 I need to bind GridView , I am using this code: ProductDBEntities db = new ProductPDBEntities(); var pro = from u in db.Products where u.PID == 1 select u; if (pro != null) { GridView1.DataSource = pro; GridView1.DataBind(); } and getting this error. System.InvalidOperationException: Sequence contains more than one element Can somebody please tell me what am I doin wrong? 回答1: Check Duplication and then try to bind it. I have edited my last answer in order to display the complete code :

entity framework 4 many to many update

安稳与你 提交于 2019-12-12 08:14:39
问题 I have 3 tables - User (Id, Name) Roles (Id, Name) UserRoles (UserId, RoleId) I think they are self explanatory. How do I update an entry (UserId and RoleId) in UserRoles? context.User.Roles gives me the list of roles but how do I update them? Thank you. 回答1: From your comment: context.User.Roles gives me the list of roles. I can do a for-each and update the Id, but how do I update the corresponding UserId foreach RoleId in that table? First of all, you should NOT update the Id's. Secondly,

Is it possible to get Entity Framework to recognize objects that have been created and not yet saved in database?

独自空忆成欢 提交于 2019-12-12 08:14:33
问题 Say Table1 is a table with two columns. Table1ID and Name. If I do the following code... var Obj1 = new Table1(); Obj1.Name = "hello" TestDBEntities.AddToTable1(Obj1); var currObj = Table1.Where(o => o.Name.Contains("hello")).FirstOrDefault(); currObj will return null. But if I do this var Obj1 = new Table1(); Obj1.Name = "hello" TestDBEntities.AddToTable1(Obj1); **TestDBEntitles.SaveChanges();** var currObj = Table1.Where(o => o.Name.Contains("hello")).FirstOrDefault(); Then currObj will

How do you assign a transaction with EF4 CodeFirst CTP5 without using TransactionScopes?

人走茶凉 提交于 2019-12-12 07:39:23
问题 I am attempting to perform functional tests against a live database, to make sure my linq queries are translating into SQL correctly. I want to do this by using transactions, so that one functional test does not affect another. The problem is, I cannot find any way in the API to utilize transactions correctly. I can retrieve a new DbTransaction via MyDbContext.Database.Connection.BeginTransaction() , however, I cannot find anyway to actually use this transaction. All documentation I can find

Entity Framework CTP5, code-first. Many to many with cascade delete

偶尔善良 提交于 2019-12-12 07:29:35
问题 I have two entities (Customer and CustomerRole) and would like to declare many-to-many relationship between them. I can do using the following code: modelBuilder.Entity<CustomerRole>() .HasMany(cr => cr.Customers) .WithMany(c => c.CustomerRoles) .Map(m => m.ToTable("Customer_CustomerRole_Mapping")); But it creates the relationship (and the third mapping table) with cascade delete switched off by default. How can I tell EF to create the relationship with cascade delete switched on when using