code-first

How can I convert an EF4 Code-First ICollection to an EntityCollection?

孤街醉人 提交于 2019-12-08 01:52:36
问题 Say I have the following entity: public class Post { public int Id { get; set; } public virtual ICollection<Comment> Comments { get; set; } } When I retrieve a Post object from the database, I need to convert the Comments collection into an EntityCollection<T> so that I can check some EF4 related data about the collection, such as if the data was eager loaded or not. Unfortunately, if I try to do a direct cast from ICollection<T> to EntityCollection<T> , I get an exception due to the fact

Pluggable Conventions in Entity Framework

♀尐吖头ヾ 提交于 2019-12-08 01:46:39
问题 I am following EF Feature CTP5: Pluggable Conventions to create custom pluggable convention (in this particular case to change the precision of all decimal fields). Looks like in the latest release of EF the Add method on ConventionsConfiguraions is also "internal'. How do i add custom Pluggable Conventions now? 回答1: The feature has been removed in EF 4.1 and a possible implementation postponed to a later release: Code First customizable (pluggable) conventions are not supported. Removing the

EF Migrations error: could not load type 'System.Data.Entity.Infrastructure.DbContextInfo'

人走茶凉 提交于 2019-12-07 14:54:28
问题 I am using ContosoUniversity example. I have just used Nuget to download and install code first migrations pakage. Whe I excecute update-database command it throws an error . Is there anything to do more than installing nuget package? Update-Database : Could not load type 'System.Data.Entity.Infrastructure.DbContextInfo' from assembly 'EntityFramework, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. At line:1 char:16 + update-database <<<< + CategoryInfo : NotSpecified: (:

Self-referencing many-to-many relationship EF code first

不羁的心 提交于 2019-12-07 11:17:17
问题 I work with ASP.NET MVC With Durandal/Breeze templates. Let's say I have the following class: public class Person { public int Id { get; set; } public string Firstname { get; set; } public string Lastname { get; set; } public virtual List<Person> Friends { get; set; } } With the following EF Fluent API: modelBuilder.Entity<Person>() .HasMany(m => m.Friends) .WithMany() .Map(m => m.ToTable("Friends")); The database is generated successfully. The problem is when I perform a que ry with Breeze

One out of 2 properties should be null (EntityFramework Code First)

扶醉桌前 提交于 2019-12-07 09:55:30
问题 I've searched a lot in the forum, but haven't found anything about regarding this issue. I have 2 properties in the EntityFramework Code First: [Column(TypeName = "Money")] public decimal? Debit { get; set; } [Column(TypeName = "Money")] public decimal? Credit { get; set; } One of them should be not null, but the other one should be null Examples: Debit=null; Credit=34; Debit=45; Credit=null; On the other hand, it should not be possible to set both or none of them null. Is it possible to

Entity Framework 4 - TPT Inheritance in Features CTP5 (code first): rename foreign key column on inherited table

ぐ巨炮叔叔 提交于 2019-12-07 08:14:32
I'm trying to convert an xml Entity Framework model to Code First (CTP5) one. I have to model a hierarchy which fits quite well the TPT pattern. The only problem I have is that the primary key/foreign key of the "inheriting" table has a different name from the primary key of the base class. These are the relevant fields of the involved tables CREATE TABLE site.Domains ( ID INT NOT NULL PRIMARY KEY, Domain NVARCHAR(128) NOT NULL ) CREATE TABLE site.MainSites ( FKDomainID INT NOT NULL PRIMARY KEY REFERENCES site.Domains(ID) ) CREATE TABLE site.SisterSites ( FKDomainID INT NOT NULL PRIMARY KEY

Revert database in Entity Framework code-first

假如想象 提交于 2019-12-07 05:32:05
问题 I made a in my model and updated database using Add-Migration sdfgfsd Update-Database Now I found the change I just made is not necessary. I would like do revert both code and database. How can I do this? 回答1: Yes - it's straightforward. Use Update-Database -TargetMigration:zzzz Where zzzz is the name of the migration before the one you want to rollback. EDIT You then need to delete the migration(s) after zzzz. If you want to rollback all migrations, or if you only have one migration, use

Entity Framework Navigation Properties looping issue though WCF

非 Y 不嫁゛ 提交于 2019-12-07 05:08:08
问题 I have a model like public class User { [Key] public long UserId { get; set; } [Required] public String Nickname { get; set; } public virtual ICollection<Group> Memberships { get; set; } } public class Group { [Key] public long GroupId { get; set; } [Required] public String Name { get; set; } public virtual ICollection<User> Members { get; set; } } public class DataContext : DbContext { public DbSet<User> Users { get; set; } public DbSet<Group> Groups { get; set; } public DataContext() {

Entity Framework CTP4 Code First: Mapping protected properties

霸气de小男生 提交于 2019-12-07 04:23:14
问题 I would like to use a lazy-loading collection on a model, but I want Add/Remove functionality to be done through separate methods. So something like this: class Model { protected virtual ICollection<Something> _somethings { get; set; } public IEnumerable<Something> Somethings { get { return _somethings; } } public void AddSomething(Something thingToAdd) { /* logic */ _somethings.Add(thingToAdd); } } I can't figure out how to configure the mapping for this. I looked into using a configuration

Entity Framework:Why the collection type of entity class need to be instanced in the default constructor?

那年仲夏 提交于 2019-12-07 02:20:16
问题 I am using Visual Studio to build a code first model of NorthWind database automatically. I have some questions. I found that if the entity class has a collection, then the is collection always instantiated in the default constructor. Why we need to do that? The ICollection<T> is instantiated as a HashSet<T> in the default constructor. Why is HashSet<T> used? Can i use List<T> or something else? Why is the navigation property at one side (one to many relation) is ICollection<T> and virtual ?