3

I have an ASP.NET application I created using the MVC template with Identity set to individual user accounts. That means I have a DbContext for Identity

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

And I have a local database that looks like this

enter image description here

I want to add a new table to my schema for credit card information. The table will be named dbo.CreditCards and will have three columns, ID int, UserName varchar(128), cardNumber char(16), expirationDate date, and CVV char(3). How can I modify this context to create that table with those columns in this schema as well?

2 Answers 2

2

It is the easiest thing to do with Entity Framework. You can learn a lot by checking the Info section of EntityFramework tag here in StackOverflow. You can read the books or visit the websites listed there.

To answer your question, just create the CreditCard entity like this:

public class CreditCard
{
    public int Id { get; set; }

    [StringLength(128)]
    public string UserName { get; set; }

    [MinLength(16), MaxLength(16)]
    [Column(TypeName = "CHAR")]
    public string CardNumber { get; set; }

    [Column(TypeName = "DATE")]
    public DateTime ExpirationDate { get; set; }

    [MinLength(3), MaxLength(3)]
    [Column(TypeName = "CHAR")]
    public string CVV { get; set; }
}

And your DbContext just add a new property called CreditCards which will be of type DbSet<CreditCard>.

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{

    public DbSet<CreditCard> CreditCards { get; set; }        

    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

If you recreate or update the database you'll have that table.

Sign up to request clarification or add additional context in comments.

2 Comments

Seems like this should work but I get this error Index 'PK_dbo.CreditCards' row length exceeds the maximum permissible length of '8060' bytes. Could not create constraint or index. See previous errors.
Its strange, commenting out the last property fixes it
2

Code-first means when you want to add something, you add it to your code first.

  • Add a class for credit card information.
  • Define class properties ID, UserName and CardNumber
  • Add public DbSet<CreditCardInfo> CreditCards { get; set; } to ApplicationDbContext
  • Your project is migration enabled since you use Identity and Identity enables migrations
  • Add a new migration from Package Manager Console with Add-Migration AnyName
  • Update database to reflect migrations from Package Manager Console again with Update-Datebase

You can refer to this tutorial for a more detailed walkthrough.

3 Comments

NuGet console called Package Manager Console
Yeah that's the actual name. I've edited to be more clear.Thanks.
You can also map your class property to your column name, incase you did not want to have the same property name and column name

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.