entity-framework-4.1

OData:Wildcard (startswith) filtering for number (ID) fields in the url request

回眸只為那壹抹淺笑 提交于 2020-01-02 22:32:21
问题 I've been researching a way to perform fuzzy searches for my numerical ID fields of my entity via OData and JavaScript. So far, I have not found the answer I am looking for. I can filter other edm.string columns perfectly using the "Startswith" filter option, however when i attempt to pass in any other non-string type, I get a type error response from the server back. In applications that I control the database, I was successfully able to get around this, by creating views I need and

How to call Database.SetInitializer on model-first?

假如想象 提交于 2020-01-02 06:31:29
问题 I created a model in the edm designer (VS10) using "DbContext Entity Generator" as the generation item. In the generated DbContext subclass, it overrode the constructor so I can't use it in the other partial class: public EntitiesContainer() : base("name=EntitiesContainer") { this.Configuration.LazyLoadingEnabled = false; } What's the proper way of initialize a database with model-first? 回答1: You could alter the T4 template that's being used for generating the DbContxt class. Then you could

Entity Framework Code First in class library

北战南征 提交于 2020-01-02 05:33:04
问题 I just started trying my hands on EF4 code first this morning and I created my POCO, data context and Initializer classes in a separate class library, I believe it's the regular boiler plate type code. I reference the class in an MVC3 application and set the initializer in the Global.asax. On running the app, I notice the following problems 1. No database is created anywhere (Then I add an entry in the web.config for a connection string named after the Context class, still no result) 2. When

Entity Framework 4.1 code-first KeyAttribute as non-identity column

梦想与她 提交于 2020-01-02 01:03:53
问题 I've got a problem with a code-first model I've got. Data is now in the database so I can't re-seed the database using a DropCreateDatabaseIfModelChanges class, but I need to change one table so that a bigint column is not an IDENTITY(1,1). I've managed to do this using SSMS but now my EF code is saying it's out of date. This is the code for the table in question: public class Vote { [Required, Key, DatabaseGenerated(DatabaseGeneratedOption.None)] public long FacebookUserId { get; set; }

Reload field value modified in DB by trigger after Insert/Update

廉价感情. 提交于 2020-01-01 19:15:58
问题 I have an entity with a string property mapped to a nvarchar field in the DB. I use an after insert/update trigger to set the value of this field. By default EF will not load the value of this field after insert/update - only identity fields are reloaded from DB after insert. I've tried to change the StoreGeneratedPattern option on this field to Computed (which seems like the right way to do that) but I get the error: The store generated pattern 'Computed' is not supported for properties that

EF Code first related entities context fail

China☆狼群 提交于 2020-01-01 16:49:29
问题 Dunno how to name this properly. I have two entities in m:n relationship: Member and Role. public class Role { public int Id { get; set; } public string Title { get; set; } public ICollection<Member> MembersInRole { get; set; } } public class Member { public int Id { get; set; } public string Name { get; set; } public string Password { get; set; } public string Email { get; set; } public ICollection<Role> Roles { get; set; } } I have made some seed data: http://i56.tinypic.com/2vjvj1w.png And

Entity Framework DateTime column - GetDate() value Insertion

[亡魂溺海] 提交于 2020-01-01 09:21:46
问题 I have a table with DateCreated and DateUpdated columns and using Entity Framework to insert/update values to the database. I need the DateCreated column to get the SQL Server's GetDate() value on insertion only. DateUpdated column value should always get updated with current GetDate() value on insertion and update both. For the DateCreated column, I've set StoreGeneratedPattern="Computed" and on SQL Server table I've set default value of the column to be GetDate(), which works nicely as

Code first - how can I save an ICollection when I'm not setting the parent object to EntityState.Modified?

╄→尐↘猪︶ㄣ 提交于 2020-01-01 06:16:27
问题 If I have the below class: public class Foo() { public int PropertyIWantUpdated {get; set;} public int PropertyIDontWantUpdated (get; set} public ICollection<Bar> Bars {get; set;} } When saving to my db, instead of context.Entry(thisFoo).State = EntityState.Modified; I'm using context.Entry(thisFood).Property(tf => tf.PropertyIWantUpdated).IsModified = true; how can I also save changes to Bars? 回答1: It depends what you are trying to update. First of all let me clarify one important fact - if

Linq to Entities SqlFunctions.DateDiff not supported

女生的网名这么多〃 提交于 2020-01-01 05:33:07
问题 I have the following code DateTime now = DateTime.UtcNow; var allItemsOver64 = _inventoryContext.Items.Where(i => (SqlFunctions.DateDiff("dd", i.PrimaryInsured.DoB, now) / 365.0) >= 65); IQueryable<Item> items65To69 = allItemsOver64.Where(i => (SqlFunctions.DateDiff("dd", i.PrimaryInsured.DoB, now) / 365.0) >= 65 && (SqlFunctions.DateDiff("dd", i.PrimaryInsured.DoB, now) / 365.0) <= 69); But when I try and use allItemsOver64 thus Items65To69.Count() I get this error The expression ((((Convert

Entity Framework Code First - Configuration in another file

落花浮王杯 提交于 2020-01-01 05:19:05
问题 What is the best way to separate the mapping of tables to entities using the Fluent API so that it is all in a separate class and not inline in the OnModelCreating method? What I am doing currently: public class FooContext : DbContext { // ... protected override OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Foo>().Property( ... ); // ... } } What i want: public class FooContext : DbContext { // ... protected override OnModelCreating(DbModelBuilder modelBuilder) {