ef-code-first

How can Entity Framework Code First Create Index in Descendent order?

不羁岁月 提交于 2019-12-10 17:36:23
问题 In fluent API, we can specify an Index on a field: var indexAttr = new IndexAttribute("IX_EmployeeNumber") { IsClustered = true }; Property(c => c.EmployeeNumber) .HasColumnAnnotation("Index", new IndexAnnotation(indexAttr )) .HasMaxLength(8) .IsRequired(); After Add-Migration , we will get this CreateIndex statement: CreateIndex("dbo.Employees", "EmployeeNumber", clustered: true, name: "IX_EmployeeNumber"); After a Update-Database , we have a sql like this: CREATE CLUSTERED INDEX [IX

Entity Framework Code First internal class - is it possible?

北战南征 提交于 2019-12-10 17:33:57
问题 Is it possible to have Code First data classes declared with internal access as shown: internal class Person { public int Id { get; set; } public string Name { get; set; } } I have a requirement that classes and its properties should not be visible outside of the assembly. 回答1: As long as your DbContext derived class that exposes your class to EF is in the same assembly, you should be able to. I don't happen to design my apps that way as I prefer more separation. But the context should be

How to create a nested GridView to edit EF Code First relation?

大兔子大兔子 提交于 2019-12-10 17:25:10
问题 I've got a classic Parent-Child relation that I would like to CRUD by using asp:GridView controls. To CRUD the parent is easy, but the challenge is to nest a asp:GridView within a asp:GridView that is able to work on the child relation. To make the problem easier, I've constructed an example. Consider the following EF-code: public class Context : DbContext { public DbSet<Animal> Animals { get; set; } public DbSet<Tag> Tags { get; set; } } public class Animal { public int AnimalID { get; set;

One to Many Relationship with Join Table using EF Code First

只愿长相守 提交于 2019-12-10 17:16:11
问题 I have a question about how to configure the one to many relationship with a join table using Code First fluent API. I have a Company, Contact Object both share a common Address Object like below Class Address { public int AddressId ..... } Class Company { public int CompanyId ...... public virtual ICollection<Address> Address } Class Contact { public int ContactID ....... public virtual ICollection<Address> Address } My expected DB structure would be Company Table CompanyId PK NOT NULL .....

EF Code First - IsConcurrencyToken()

喜你入骨 提交于 2019-12-10 17:06:48
问题 Simple, but yet mysterious for me: Why do StringPropertyConfiguration (and all the other PropertyConfiguration) class(es) have 2 overloads for IsConcurrencyToken() ? The first: public StringPropertyConfiguration IsConcurrencyToken() Configures the property to be used as an optimistic concurrency token. And the second: public StringPropertyConfiguration IsConcurrencyToken(bool?) Configures whether or not the property is to be used as an optimistic concurrency token. Why would you use one over

How to Get Full Object with Code First Entity Framework 4.1

心已入冬 提交于 2019-12-10 16:55:43
问题 I am trying to return as JSON the fully deep object (with all of the foreign key relationships filled in) but I am getting nulls for all the referenced objects. Here is the call to get the object: public ActionResult GetAll() { return Json(ppEFContext.Orders, JsonRequestBehavior.AllowGet); } And here is the Order object itself: public class Order { public int Id { get; set; } public Patient Patient { get; set; } public CertificationPeriod CertificationPeriod { get; set; } public Agency Agency

Entity Framework Code First One-To-Many Relation Mapping with link table

徘徊边缘 提交于 2019-12-10 16:50:05
问题 I have a question about One-To-Many relationship between two tables when there is the link (join) table between. Example tables: ChildTable: ID int NOT NULL PK Relation int NOT NULL ParentTable: ID int NOT NULL PK Name nvarchar(50) NOT NULL ParentChildren: ParentTable_ID int NOT NULL PFK ChildTable_ID int NOT NULL PFK Entities: public class ChildTable { public ChildTable() { this.ParentTables = new List<ParentTable>(); } public int ID { get; set; } public int Relation { get; set; } public

Entity Framework Code First - Cannot insert duplicate key in object 'dbo.T_CRProviders'

天涯浪子 提交于 2019-12-10 16:43:29
问题 I have some urgent issue which I could not find answer for across the web. I am using CodeFirst EF 4.3.1 and I am getting an error: Violation of PRIMARY KEY constraint 'PK_T_CRProviders'. Cannot insert duplicate key in object 'dbo.T_CRProviders'. My code is: Models: public enum CRProviderEnums { PE_Abcd = 0, PE_Efgh } [Table("T_CRProviders")] public class CRProvider { [Key] [Required] public int Enum { get; set; } [Required] public string Name { get; set; } } [Table("T_CRSupportedResources")]

EDMX for legacy code _and_ Code First for new development together in one MVC project

泪湿孤枕 提交于 2019-12-10 16:17:23
问题 The situation as follows: we have one big MVC project with database first approach on EF5.0: ObjectContext constructor: namespace xxx.Models { (...) public partial class xxxEntities : ObjectContext { #region Constructors /// <summary> /// (...) /// </summary> public xxxEntities() : base("name=xxxEntities", "xxxEntities") { this.ContextOptions.LazyLoadingEnabled = true; OnContextCreated(); } (...) connection string: <add name="xxxEntities" connectionString="metadata=res://*/Models.xxxModel

Entity Framework Code First TPH inheritance - can different child classes share a field?

我们两清 提交于 2019-12-10 15:57:09
问题 I have an Entity Framework Model created using Entity Framework Code First that is using Table Per Hierarchy inheritance where the structure looks a little like this: public abstract class BaseState { public int Id { get; set; } public string StateName { get; set; } // etcetera } public class CreatedState : BaseState { public User Owner { get; set; } } public class UpdatedState : BaseState { public User Owner { get; set; } } Now what that creates is in my BaseStates table I have Owner_Id and