entity-framework-5

Entity Framework Code First Migration Class File

你说的曾经没有我的故事 提交于 2019-12-05 21:41:45
I'm building an application using ASP.NET MVC 4 with Entity Framework 5.0. I've recently learned about EF Code-First Migrations tool you can implement on your application for your database. I ran "Enable-Migration" in the PM Console, and it enabled successfully and created the "InitialCreate.cs" class as expected with the appropriate up() and down() methods filled in with the table creations. Then I went ahead and added another class (Category.cs) with 2 simple properties (id, name) and wanted to add this migration to the database. I ran "Add-Migration AddCategoryClass" in my PM to get this

Entity Framework 5 - Migrations and creating table from entity

情到浓时终转凉″ 提交于 2019-12-05 21:39:56
I am using Entity Framework 5 RC and i have some code that require a specific table on the database. The entity is already created using Code-First. a) Is there a way to tell EF to create the table if its not already created in the database ? If yes.. how ? b) Also.. if the table already exist, can it handle entity changes such adding properties to the entity.. will it get reflected on the database ? (We still use the code-first approach here) Thanks! Ladislav Mrnka Use code first migrations (either automatic or code based ). If you are adding table to existing database create initial

Get ignored properties in Entity Framework

三世轮回 提交于 2019-12-05 19:48:56
I work on a framework with EF. I want to get all ignored properties of an entity to build some special queries. How can I do it? public class Customer { public int Id { get; set; } public DateTime BirthDate { get; set; } public int Age { get; set; } } public class CustomerContext : DbContext { protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Customer>().Ignore(customer => customer.Age); base.OnModelCreating(modelBuilder); } public DbSet<Customer> Customers { get; set; } } public static class DbContextExtensions { public static List<string>

Code first columns with type char(36)

瘦欲@ 提交于 2019-12-05 19:20:17
问题 So I have a UserProfile model class as part of SimpleMembership. In it I need to store a legacy identifier that exists in another DB of type char(36) . I'd love to change this to something more sensible like a uniqueIdentifier but that's out of scope for today's activities. My current annotation creates a column nvarchar(36) [StringLength(36)] public string UserIdentifier{ get; set; } I'd like a column of char(36) instead. Is this possible? 回答1: If you want to keep with Data Annotations, then

EF linq/lambda .contains(list[String])?

允我心安 提交于 2019-12-05 19:05:22
is there any way to evaluate if a string contains some of the elements of a list or all the elements of the list? using linq to entities? i have been trying to use predicateBuilder and others but im not %100 into thoses. EDIT something like: string[] words = searchString.Split(' '); var resultado = db.users .Where(u => u.fullName.contains(words) ) .Select(s => new { user_id = s.id_user, nombre = s.fullName}) .ToList(); You need to reverse your use of Contains to check the words collection for the fullName : string[] words = searchString.Split(' '); var resultado = db.users .Where(u => words

Inserting current Database date time via Entity framework

江枫思渺然 提交于 2019-12-05 18:51:22
We want to put the current datetime into a database column. We have a web farm where the time on the web servers can vary, we therefore need to use the datetime on the database server. We have a database with a Not Null datetime column. This column has a default datetime of current time. This works fine when we insert data using a SQL statement that does not include the datetime column. From Entity Framework: If we define the datetime as not null , the datetime is set to low date, and low date is inserted into the database. If we define the datetime as null , we get an exception on the insert

How to get the next Sequence number in Sql Server Express using Entity Framework?

蹲街弑〆低调 提交于 2019-12-05 18:32:28
问题 Now that Sql Server 2012 (including SQL Server Express 2012) has SEQUENCE feature just like Oracle as explained here, here, and here. I can get the next sequence like so, SELECT NEXT VALUE FOR SeqName But how do I do that from my code using Entity Framework 5? 回答1: I got it working using SqlQuery like so.. int sequence = context.Database.SqlQuery<int>("SELECT NEXT VALUE FOR MySequenceName").FirstOrDefault(); 来源: https://stackoverflow.com/questions/16753149/how-to-get-the-next-sequence-number

NullReferenceException in EF 5-6.1.1 with two navigation properties to the same type

房东的猫 提交于 2019-12-05 17:50:48
I'd like to start with that I have a workaround for this issue - but I spent a few hours today figuring out the cause of the exception, so I'd thought I'd share Given two entities in the domain: public class User { public int Id { get; set; } public string Name { get; set; } } public class Ticket { public int Id { get; set; } public string Name { get; set; } public virtual User Owner { get; set; } public int? LockedByUserId { get; set; } public virtual User LockedByUser { get; set; } [Timestamp] public byte[] ETag { get; set; } } The following configuration: public class TicketConfiguration :

Is it possible to create a generic method for adding items to a entity framework dbset?

只谈情不闲聊 提交于 2019-12-05 17:49:22
问题 I have not worked with Entity Framework or generics before and am having some difficulty reducing my code. I am parsing a text file to load 10 lookup tables with data that could possibly change nightly. The text files have a heading for the "type" followed by a list of key/value sets. I have this working perfectly, but I would like to refactor the code to clean it up and would like to use generic methods to accomplish this so I can reduce the duplicated code. I have gotten the parsing down to

How to specify a column Name for EF5 navigation property

时光毁灭记忆、已成空白 提交于 2019-12-05 17:42:58
I'm using EF5 code first to generate my database schema, but my new navigation property is being named in an undesirable way in the table. here is the model I'm working with. public class User { [Key] public long UserId { get; set; } ... **public virtual ICollection<ImagePermission> KeepThisNavigationName { get; set; }** } However, After I've updated my database and examine the table columns, the column is named: dbo.ImagePermission.User_UserId And I would like it to be named dbo.ImagePermission.KeepThisNavigationName_UserId I believe there is a way to do this using the Fluent API, but after