entity-framework-5

entity framework 5 MaxLength

吃可爱长大的小学妹 提交于 2019-11-30 03:55:11
I was using EF4 and a piece of code I found to get the MaxLength value from an entity like this: public static int? GetMaxLength(string entityTypeName, string columnName) { int? result = null; using (fooEntities context = new fooEntities()) { Type entType = Type.GetType(entityTypeName); var q = from meta in context.MetadataWorkspace.GetItems(DataSpace.CSpace) .Where(m => m.BuiltInTypeKind == BuiltInTypeKind.EntityType) from p in (meta as EntityType).Properties .Where(p => p.Name == columnName && p.TypeUsage.EdmType.Name == "String") select p; var queryResult = q.Where(p => { bool match = p

Entity Framework - getting a table's column names as a string array

瘦欲@ 提交于 2019-11-30 03:08:52
If I'm using EF 5 and Database first to generate a .edmx model of my database, how do I get a list of an entity's columns? using (var db = new ProjectNameContext()) { // string[] colNames = db.Users. } What I'm looking for is colNames[0] == "Id", colNames[1] == "FirstName", etc. How about: var names = typeof(User).GetProperties() .Select(property => property.Name) .ToArray(); Of course, this can be used for any type, not just an EF table. var res = typeof(TableName).GetProperties() .Select(property => property.Name) .ToArray(); OR var res = dbContext.Model.FindEntityType(typeof(TableName))

How can I resolve this error: Loading the include file 'EF.Utility.CS.ttinclude' returned a null or empty string

家住魔仙堡 提交于 2019-11-30 02:57:20
How can I resolve these errors: Loading the include file 'EF.Utility.CS.ttinclude' returned a null or empty string. Value does not fall within the expected range. Tools Used: VS 2012, Entity Framework 5.0.0, T4MVCExtensions 3.5.0, .NET Framework 4.5 I've been using EF and T4 templates in a project within a large MVC solution without incident. Now, when I right-click on the edmx diagram, EF responds with the message: Value does not fall within the expected range. When I compile the EF project it responds with these messages: Loading the include file 'EF.Utility.CS.ttinclude' returned a null or

EntityFramework 4 upgraded to 5, lambda is not available

天大地大妈咪最大 提交于 2019-11-30 02:44:55
问题 I have upgraded my "entityframework 4" project to 5. I want to use lambda expression in Include (my motivation is to suplant string definitions) brackets. At this momemnt I have: context.WarrantyContract.Include("Car"); And want to achieve this one: context.WarrantyContract.Include(w => w.Car); But when I try to replace string, visual studio is not eable to recognize my will. I'll appreciate any right direction. 回答1: The lambda version of the Include is declared in the System.Data.Entity

Filtering navigation properties in EF Code First

你说的曾经没有我的故事 提交于 2019-11-30 02:40:33
问题 I'm using Code First in EF. Let's say I have two entities: public class Farm { .... public virtual ICollection<Fruit> Fruits {get; set;} } public class Fruit { ... } My DbContext is something like this: public class MyDbContext : DbSet { .... private DbSet<Farm> FarmSet{get; set;} public IQueryable<Farm> Farms { get { return (from farm in FarmSet where farm.owner == myowner select farm); } } } I do this so that each user can only see his farms, and I don't have to call the Where on each query

How can I use EF to add multiple child entities to an object when the child has an identity key?

拜拜、爱过 提交于 2019-11-30 02:15:28
问题 We are using EF5 and SQL Server 2012 the following two classes: public class Question { public Question() { this.Answers = new List<Answer>(); } public int QuestionId { get; set; } public string Title { get; set; } public virtual ICollection<Answer> Answers { get; set; } } public class Answer { public int AnswerId { get; set; } public string Text { get; set; } public int QuestionId { get; set; } public virtual Question Question { get; set; } } Mapping is as follows: public class AnswerMap :

Entity Framework Migrations don't include DefaultValue data annotation (EF5RC)

ε祈祈猫儿з 提交于 2019-11-30 01:39:43
问题 I have a class that looks like this: [Table("Subscribers", Schema = "gligoran")] public class Subscriber { [Key] public string Email { get; set; } [Required] [DefaultValue(true)] public bool Enabled { get; set; } } When creating a migration to include this class I get: public partial class AddSubscriberClass : DbMigration { public override void Up() { CreateTable( "gligoran.Subscribers", c => new { Email = c.String(nullable: false, maxLength: 128), Enabled = c.Boolean(nullable: false), })

EF5 Migrations - Duplicate/Re-defined variable bug when dropping constraints | Issue with usage of SQL GO command

六眼飞鱼酱① 提交于 2019-11-30 01:30:40
问题 Background: We have a project that uses ef-migrations containing multiple (read ~60) migrations created over a long period of development. Naturally, some of these migrations also involve: dropping constraints 1,2 creating triggers All is unicorns and rainbows when we run Update-Database because each migration runs as a separate batch. But when creating SQL Scripts for these migrations using Update-Database -Script we encountered a few issues as described below: Problem 1: When dropping

Create fulltext index within Entity Framework Coded Migrations

青春壹個敷衍的年華 提交于 2019-11-30 01:24:18
问题 TLDR; How do you add a full text index using Entity framework 5 coded migrations I'm having issues adding a full text index to a database using Entity framework migrations. It needs to be there from the start so I'm attempting modifying the InitialCreate migration that was automatically generated to add it. As there isn't a way to do it via the DbMigrations API I've resorted to running inline sql at the end of the 'Up' code. Sql("create fulltext catalog AppNameCatalog;"); Sql("create fulltext

Can I store enums as strings in EF 5?

ε祈祈猫儿з 提交于 2019-11-30 00:50:17
问题 We have been using EF CF for a while in our solution. Big fans! Up to this point, we've been using a hack to support enums (creating an extra field on the model; ignore the enum durring mapping; and map the extra field to the column in the db that we would have used). Traditionally we have been storing our enums as strings(varchars) in the DB (makes it nice and readable). Now with enum support in EF 5 (Beta 2) it looks like it only supports mapping enums to int columns in the DB....Can we get