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

Community
  • 1
  • 1
ADO_kg
  • 65
  • 2
  • 13
  • tried following: public class Region { public int RegionId { get; set; } public string RegionName { get; set; } public virtual ICollection 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 '14 at 17:51
  • Are you sure that migration is enabled? – CodeNotFound Jan 15 '14 at 18:39
  • Yes, I am sure. becuase I am doing with Code First. and other tables are updating. – ADO_kg Jan 16 '14 at 07:36

1 Answers1

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?

granadaCoder
  • 26,328
  • 10
  • 113
  • 146
  • 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 Regions { get; set; } public DbSet Zones { get; set; } } – ADO_kg Jan 16 '14 at 07:39
  • Deleted related tables from DB and run project, now tables are created in correct way as I want. tnx – ADO_kg Jan 16 '14 at 08: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. – granadaCoder Jan 16 '14 at 14:13