0

I first created following classes and updated to database, tables are created already.

public class Region
{
   public int RegionId { get; set; }
   public string RegionName { get; set; }
}

public class Zone
{
    public int ZoneId { get; set; }
    public string ZoneName { get; set; }
}

after i needed to insert foreign key into table Zone:

public class Zone
 {
    public int ZoneId { get; set; }
    public string ZoneName { get; set; }

    public virtual Region Region { get; set; }
}

tried: add-migration zone, and update-database.. it is not updating database. And Add-Migration is creating class with empty properties Up() & Down(). Also tried to add [ForeignKey("RegionId")] with no success. What I am doing wrong ?

EDIT: this is last version which is working:

public class Region
{
   public int RegionId { get; set; }
   public string RegionName { get; set; } 
}
public class Zone
{
    public int ZoneId { get; set; }
    public string ZoneName { get; set; }
    public int  RegionId { get; set; }
    public virtual Region Region { get; set; }
}

Also I had two Contexts, and found solution here (Answer of Brice) - EF 4.3 Auto-Migrations with multiple DbContexts in one database

3
  • tried following: public class Region { public int RegionId { get; set; } public string RegionName { get; set; } public virtual ICollection<Zone> Zones {get; set; } } public class Zone { public int ZoneId { get; set; } public string ZoneName { get; set; } public int RegionId { get; set; } public Region Region { get; set; } }
    – ADO_kg
    Jan 15, 2014 at 17:51
  • Are you sure that migration is enabled? Jan 15, 2014 at 18:39
  • Yes, I am sure. becuase I am doing with Code First. and other tables are updating.
    – ADO_kg
    Jan 16, 2014 at 7:36

1 Answer 1

1

The best way to learn "Code First" with an existing database...is to use the EF Power Tools.

And "reverse engineer".

http://visualstudiogallery.msdn.microsoft.com/72a60b14-1581-4b9b-89f2-846072eff19d

I learned more about how Mapping's work with this tool in 30 minutes, then I did with hours of googling.

Basically, create a "class library", and when this plugin is installed, you get a new context menu (on the project), that will allow you to reverse-engineer a database...

I love this tool.

Having said that, I usually see the FK scalar (of the parent) on the child object.

public class Zone
 {
    public int ZoneId { get; set; }
    public string ZoneName { get; set; }

    public int RegionID { get; set; }
    public virtual Region Region { get; set; }
}

But a more basic question is how are you doing your mappings?

3
  • thanks for suggestion, will download and learn that tool. putting code in comments is not looking nice, anyway: public class SiteContext : DbContext { public SiteContext() : base("RID") { } public DbSet<Region> Regions { get; set; } public DbSet<Zone> Zones { get; set; } }
    – ADO_kg
    Jan 16, 2014 at 7:39
  • Deleted related tables from DB and run project, now tables are created in correct way as I want. tnx
    – ADO_kg
    Jan 16, 2014 at 8:47
  • Maybe append your question with the final code........(leave the original question intact). It's hard to see what is happening in the comments area (when its code). Glad it worked out. Jan 16, 2014 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.