code-first

Fluent API, many-to-many in Entity Framework Core

自作多情 提交于 2019-12-17 07:14:48
问题 I've searched stackoverflow for a proper solution on generating a many-to-many relationship, using EF Core, Code first and Fluent API. A simple scenario would be: public class Person { public Person() { Clubs = new HashSet<Club>(); } public int PersonId { get; set; } public virtual ICollection<Club> Clubs { get; set; } } public class Club { public Club() { Persons = new HashSet<Person>(); } public int ClubId { get; set; } public virtual ICollection<Person> Persons { get; set; } } Please

Entity Framework Code First Date field creation

一个人想着一个人 提交于 2019-12-17 07:11:45
问题 I am using Entity Framework Code First method to create my database table. The following code creates a DATETIME column in the database, but I want to create a DATE column. [DataType(DataType.Date)] [DisplayFormatAttribute(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")] public DateTime ReportDate { get; set; } How can I create a column of type DATE , during table creation? 回答1: The EF6 version of David Roth's answer is as follows: public class DataTypePropertyAttributeConvention :

Understanding ForeignKey attribute in entity framework code first

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-17 06:36:51
问题 See the following post for some background: Entity framework one to zero or one relationship without navigation property I had always thought that ForeignKey was used to show which property in a class held the ForeignKey that determined the navigation property e.g. public class MemberDataSet { [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public int Id { get; set; } public int? DeferredDataId { get; set; } [ForeignKey("DeferredDataId")] public virtual DeferredData

Unique Constraint in Entity Framework Code First

爱⌒轻易说出口 提交于 2019-12-17 02:07:30
问题 Question Is it possible to define a unique constraint on a property using either the fluent syntax or an attribute? If not, what are the workarounds? I have a user class with a primary key, but I would like to make sure the email address is also unique. Is this possible without editing the database directly? Solution (based on Matt's answer) public class MyContext : DbContext { public DbSet<User> Users { get; set; } public override int SaveChanges() { foreach (var item in ChangeTracker

Error when using Code First in web service “System.Data.ProviderIncompatibleException: The provider did not return a ProviderManifestToken string.”

你。 提交于 2019-12-14 02:10:44
问题 I'm using Code First. Everything works just fine (inserts, updates, selects) everything is tested. The problem comes when I try to use the web services. I get the error "System.Data.ProviderIncompatibleException: The provider did not return a ProviderManifestToken string.". Looking at the inner exception I get this message "Could not determine storage version; a valid storage connection or a version hint is required.". The web service Code: /// <summary> /// Summary description for

In EF Code First is it possible to encapsulate properties that should be persisted to the data store?

余生长醉 提交于 2019-12-13 17:25:11
问题 So let's say I've got a POCO that I want EF Code First to persist to a data store: public class Campaign { public string Name { get; set; } public double GoalAmount { get; set; } private double AmountRaised { get; set; } public bool GoalMet { get { return AmountRaised >= GoalAmount; } } } Now, for whatever reason, I don't want the AmountRaised property to be accessible outside the object, but I do want it persisted to the data store. Is this possible with EF Code First? 回答1: You can make it

Entity Framework 4.1 Code-First approach to realize many-to-many relation over Domain-Services

♀尐吖头ヾ 提交于 2019-12-13 15:14:10
问题 I had some problems while creating a database model using the newest Entity Framework and Code-First (see Entity Framework 4.1 Code First approach to create many-to-many relation for details). Meanwhile I've figured out that the problem isn't the Entity Framework itself any more, but using it along with WCF RIA DomainServices. For the sake of completeness - that's my relevant Code-First code: // // Models // public class Author { public Author() { this.Books = new Collection<Book>(); }

The specified LINQ expression contains references to queries that are associated with different contexts

青春壹個敷衍的年華 提交于 2019-12-13 13:24:39
问题 I'm getting an error when trying to join against multiple tables in a query: The specified LINQ expression contains references to queries that are associated with different contexts It's confusing because it makes it seem like I'm using different contexts within the query but I'm not: public static IQueryable<Company> GetAll(bool supportsMMAT) { return from c in Context.Companies join v in Context.Vehicles on c.CompanyIdNumber equals v.CompanyIdNumber join mt in Context.ModemTypes on v

EF4 CTP5 Code First approach ignores Table attributes

坚强是说给别人听的谎言 提交于 2019-12-13 09:17:18
问题 I'm using EF4 CTP5 code first approach but am having trouble getting it to work. I have a class called "Company" and a database table called "CompanyTable". I want to map the Company class to the CompanyTable table, so have code like this: [Table(Name = "CompanyTable")] public class Company { [Key] [Column(Name = "CompanyIdNumber", DbType = "int")] public int CompanyNumber { get; set; } [Column(Name = "CompanyName", DbType = "varchar")] public string CompanyName { get; set; } } I then call it

with ef 4.1 code first how can i create a nullable column

馋奶兔 提交于 2019-12-13 07:42:52
问题 I have the following POCO: Public Class T1 <Required()> <MaxLength(128)> <Key(), Column(Order:=0)> Property K1 As String <Required()> <MaxLength(128)> <Key(), Column(Order:=1)> Property K2 As String <Required()> Property C1 As String Property C2 As String end Class I would expect C2 to be created as Nullable, but both C1 and C2 are Not Null. Adding <Required(AllowEmptyStrings:=True)> Does not make a difference, as it seems the decoration is aimed at data validation, not DB creation. So how do