entity-framework-5

Using Enums with Code First & Entity Framework 5

孤者浪人 提交于 2019-12-06 21:21:27
问题 Just trying to confirm an impression: it seems enums in EF5 + Code First are only supported when declared within the same namespace as the classes using them as property types. Can anyone confirm that? Couldn't find anything on the web for this... 回答1: A relevant bug that was fixed earlier. 4.3 Beta 1 release notes say: Bug fix for GetDatabaseValues . In earlier releases this method would fail if your entity classes and context were in different namespaces. This issue is now fixed and the

Decimal out of range

北慕城南 提交于 2019-12-06 19:59:38
问题 I'm trying to store the decimal 140.2705893427 into a SQL Server 2012 table. The column has a data type of decimal(12, 10) but I get the error: {"Parameter value '140.2705893427' is out of range."} Why is this? 回答1: decimal(12, 10) means 12 total digits, 10 of which may be after the decimal point. Your value of 140.2705893427 has 13 total digits, thus it is out of range. Read decimal and numeric (Transact-SQL) for documentation. 来源: https://stackoverflow.com/questions/18874410/decimal-out-of

How to Change the name of a primary key in EF Code First?

梦想的初衷 提交于 2019-12-06 19:06:26
问题 I have a scenario where i would like to change the primary key name in an entity and be able to run update-database -force. See below for code and error getting when i try. Entity was: public class Team { [Key] [HiddenInput(DisplayValue = false)] public virtual int Id { get; set; } [Display(Name = "Full Name:")] public virtual string Name { get; set; } } Entity Changed to: public class Team { [Key] [HiddenInput(DisplayValue = false)] public virtual int TeamId { get; set; } [Display(Name =

Entity Framework - Selecting specific columns

此生再无相见时 提交于 2019-12-06 16:00:18
问题 I have a successful query that links two tables with a where and orderby clause, but I wanted to add to just select specific columns instead of getting everything back. PART 1 When I attempt this I get syntax errors on the orderby line, if I remove the orderby line the syntax errors move to the where line. Error 3 Cannot implicitly convert type 'System.Linq.IOrderedQueryable' to 'System.Linq.IQueryable'. An explicit conversion exists (are you missing a cast?) IQueryable<VendorProfile> query =

EntityFramework t4 template - XML documentation

柔情痞子 提交于 2019-12-06 13:14:35
I have the following problem with my EDMX file. In it I have written some Documentation for the properties as well as the Entities, but the t4 template of my EF 5 doesnt generate those values. My desired result should be public class Person { /// <summary> /// Hello World Summary!! /// </summary> /// <remarks> /// Hello World Remarks!! /// </remarks> public string Name { get; set; } } But I only get public class Person { public string Name { get; set; } } And if not for this, why would I else be able to set documentation properties in the edmx file. It does appear that entity framework isn't

Ignore Properties in OnModelCreating

泄露秘密 提交于 2019-12-06 12:34:09
问题 I'm attempting to use Bounded (Db) Contexts in Entity Framework 5.0, and I'm having problems excluding a property from one of the classes included in a specific context. Here is the information (I'll shorten for brevity) BaseContext.cs public class BaseContext<TContext> : DbContext where TContext : DbContext { static BaseContext() { Database.SetInitializer<TContext>(null); } protected BaseContext() : base("name=Development") { } } IContext.cs public interface IContext : IDisposable { int

Entity Framework get user from contex in saveChanges

a 夏天 提交于 2019-12-06 11:27:13
i have two projects in my solution, UI as mvc and class project for entitiy model code first. I have severall entities in my model but now I need to extend them by new audit fields where I need to save who changed entity. I added new interface public interface IAuditable { /// <summary>Gets or sets the name.</summary> /// <value>The name.</value> DateTime CreatedDate { get; set; } /// <summary>Gets or sets the name.</summary> /// <value>The name.</value> string CreatedBy { get; set; } /// <summary>Gets or sets the name.</summary> /// <value>The name.</value> DateTime UpdatedDate { get; set; }

Get an Entity Object with its child Entity with a condition (using Dynamic Linq)

亡梦爱人 提交于 2019-12-06 10:24:45
This question is continuation of Getting Count() property in Dynamic Lambda Expression I had asked if we can put Count() Method in dynamic lambda expression. I could do it using Dynamic Expression API . Answer provided by - xmojmr But recently I had to implement a Dynamic Lambda Expression to get result with its child entity with filtered data. Details : I have a parent entity with its child entity linked. Now when I get data from parent entity, it returns a collection of data with its child data too. as per my previous requirement I had to count all child record returned with respect to each

Entity Framework: Store entity property in json format

人走茶凉 提交于 2019-12-06 09:53:07
Let's say you've an entity that has a property typed as ICollection<string> and I want to store it as varchar in SQL Server in JSON format. How to achieve this? Thank you in advance. Probably You should have another string property in your Model class to hold the JSON represntation of the Collection of string. and that property will be mapped to your Table. Do not map the Collection property to your Table. Something like this public class Customer { public int ID { set;get;} public string JsonData { set;get;} [NotMapped] public ICollection<string> Items { set;get;} } And Before saivng the data

Should we use Data Repository pattern in MVC application using Entity Framework Code First approach?

偶尔善良 提交于 2019-12-06 09:35:57
问题 I have developed many application now with Entity Framework Code First approach. In all of the I use Data Repository pattern. This Data Repository pattern queries over single entity at a Time. for e.g, I have 2 models Employee and Department So to fetch all employees and department I would create 2 data repository instances. e.g var empRepository = new DataRepository<Employee>(); var allEmployees = empRepository.GetAll(); var depRepository = new DataRepository<Department>(); var alldepartment