0

That solution does not work in .net core 2 tied to SQL Server 2016. Original Question: Trying to jump into entity framework, using the "Code First Approach". I have my new class setup “NewTable” shown below. I can’t figure out what in the Program Manger Console I need to type to get this table created in my Default Connection String (pointing to a local instance of Sql Server 2016). The database is working and the user in this .net core 2 web app can register his/her name then log in using that new created account. Thanks in advance!!

Default Connection String:

"ConnectionStrings": { "DefaultConnection" "Server=localhost\RCDSE_Dev;Database=Some Database;User ID=sa;Password=SomePass;" },

[Table("NewTable")] public class NewTable { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] //Database generated key [Key] //Primary Key public long Id { get; set; } [Required] public string Manager { get; set; } } public class NewTableContext : DbContext { public DbSet<NewTable> NewTables{ get; set; } } 
9
  • Add-Migration .. , Update-Database. More information here Commented Dec 3, 2017 at 19:27
  • Possible duplicate of How to create database using code first migrations? Commented Dec 3, 2017 at 19:28
  • PM> Add-Migration then PM>Update-Database Commented Dec 3, 2017 at 19:29
  • Add-Migration [name of migration] Then yes Update-Database Commented Dec 3, 2017 at 19:31
  • Do you want your tool to create/update the database automatically when it runs? Commented Dec 3, 2017 at 19:32

2 Answers 2

0

Add a constructor to the DbContext as below:

public class NewTableContext : DbContext { public DbSet<NewTable> NewTables{ get; set; } public NewTableContext() : base("DefaultConnection") { } } 
Sign up to request clarification or add additional context in comments.

1 Comment

This is .net core 2.. that doesn't work <code>public EngagementContext(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer("DefaultConnection"); } </code>
0

I figured this out. Will post a detailed step by step guide for others in the next couple of days. There are specific things that need to be done in .net core 2 project file that were NOT COVERED in any of the referenced examples by users here. Thanks for all the help..it got me somewhat pointed in a direction, to find the right direction...lol.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.