create FK to model, from aspnet forms validation tables

172 Views Asked by At

I've recently created a MVC 3 ASP.net application (using VB). I have also created forms validation by running aspnet_regsql. I used this to create the table in the same database as my application uses. I am trying to work out how to create a new model, with a FK from the Users table. Do I have to reverse engineer the validation tables to create models in my application? right now, the tables only exist on the SQL 2008 database, as they were created by running the aspnet_regsql command, and do not exists as models in my ASP.net solution. Here is the code I have now, for a model I would like to add a FK to (as a one-to-many relationship):

  Imports System.Data.Entity
  Imports System.ComponentModel
  Imports System.ComponentModel.DataAnnotations
  Imports System.Globalization

  Public Class modelArtist
    Public Property ID() As Integer

    <Required()> _
    <DataType(DataType.Text)> _
    <Display(Name:="Artist Name:")> _
    Public Property ArtistName() As String

    <DataType(DataType.Text)> _
    <Display(Name:="Artist Bio:")> _
    Public Property ArtistBio() As String

   <Display(Name:="")>
   Public Property dateCreated() As Date

   <Display(Name:="")> _
   Public Property dateLastEdited() As Date
 End Class

  Public Class iDjItDBContext
   Inherits DbContext
   Public Property modelArtist() As DbSet(Of modelArtist)
  End Class
1

There are 1 best solutions below

0
On BEST ANSWER

You can add simple property representing FK to Users table but you should not add navigation property. FK in this case will be just simple property with no other meaning. It will make the connection between your entity and user but it will not allow you working with Users from EF - that is exactly what you want. You will also have to create relation in database between your table and Users table manually.

The point is don't map anything from ASP.NET database tables.

  • Those features have its own API in ASP.NET. Accessing those features from EF will violate encapsulation of that API (as well as separation of concerns).
  • Classes provided by that API usually cannot be mapped directly. If you would like to map tables from database you could need to make new entity classes for those entities = code duplication and again breaking encapsulation and rules enforced by that API.

Btw. it is called forms authentication not forms validation.