0

当我尝试使用以下代码创建用户时,出现以下异常。

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Register(RegisterModel model, string returnurl) {
    if (ModelState.IsValid) {
        WebSecurity.CreateUserAndAccount(model.Emailaddress, model.Password, propertyValues: new {
            Firstname = model.Firstname,
            Lastname = model.Lastname,
            Activated = true
        });
        Roles.AddUserToRoles(model.Emailaddress, Roles.GetAllRoles()); 
        return RedirectToAction("Login", new { returnurl = returnurl });
    }
    return View(model);
}

System.Data.SqlClient.SqlException wurde nicht von Benutzercode behandelt.
  HResult=-2146232060
  Message=Invalid column name 'RoleId'.
Invalid column name 'UserId'.
  Source=.Net SqlClient Data Provider
  ErrorCode=-2146232060
  Class=16
  LineNumber=1
  Number=207
  Procedure=""
  Server=(LocalDb)\v11.0
  State=1
  StackTrace:
       bei System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
       bei System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
       bei System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
       bei System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
       bei System.Data.SqlClient.SqlDataReader.get_MetaData()
       bei System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
       bei System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite)
       bei System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
       bei System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
       bei System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
       bei System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
       bei WebMatrix.Data.Database.<QueryInternal>d__0.MoveNext()
       bei System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
       bei WebMatrix.WebData.DatabaseWrapper.QuerySingle(String commandText, Object[] parameters)
       bei WebMatrix.WebData.SimpleRoleProvider.IsUserInRole(String username, String roleName)
       bei WebMatrix.WebData.SimpleRoleProvider.AddUsersToRoles(String[] usernames, String[] roleNames)
       bei System.Web.Security.Roles.AddUserToRoles(String username, String[] roleNames)
       bei Coding.Lizards.Video.Manager.Web.Controllers.AccountController.Register(RegisterModel model, String returnurl) in c:\Users\Kirk\Documents\Bitbucket\video-and-stats-manger-webapp\video-and-stats-manager\Coding.Lizards.Video.Manager.Web\Controllers\AccountController.cs:Zeile 61.
       bei lambda_method(Closure , ControllerBase , Object[] )
       bei System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
       bei System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
       bei System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
       bei System.Web.Mvc.Async.AsyncControllerActionInvoker.InvokeSynchronousActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
       bei System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41()
       bei System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _)
       bei System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
       bei System.Web.Mvc.Async.AsyncResultWrapper.End[TResult](IAsyncResult asyncResult, Object tag)
       bei System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
       bei System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33()
       bei System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49()
  InnerException: 

这就是我的 SQL 脚本的样子

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'User'
CREATE TABLE [dbo].[User] (
    [UserId] int IDENTITY(1,1) NOT NULL,
    [Firstname] nvarchar(max)  NOT NULL,
    [Lastname] nvarchar(max)  NOT NULL,
    [Emailaddress] nvarchar(max)  NOT NULL,
    [Activated] bit  NOT NULL,
    [Profilepicture] varbinary(max)  NULL
);
GO

-- Creating table 'webpages_Membership'
CREATE TABLE [dbo].[webpages_Membership] (
    [UserId] int  NOT NULL,
    [CreateDate] datetime  NULL,
    [ConfirmationToken] nvarchar(128)  NULL,
    [IsConfirmed] bit  NULL,
    [LastPasswordFailureDate] datetime  NULL,
    [PasswordFailuresSinceLastSuccess] int  NOT NULL,
    [Password] nvarchar(128)  NOT NULL,
    [PasswordChangedDate] datetime  NULL,
    [PasswordSalt] nvarchar(128)  NOT NULL,
    [PasswordVerificationToken] nvarchar(128)  NULL,
    [PasswordVerificationTokenExpirationDate] datetime  NULL
);
GO

-- Creating table 'webpages_OAuthMembership'
CREATE TABLE [dbo].[webpages_OAuthMembership] (
    [Provider] nvarchar(30)  NOT NULL,
    [ProviderUserId] nvarchar(100)  NOT NULL,
    [UserId] int  NOT NULL
);
GO

-- Creating table 'webpages_Roles'
CREATE TABLE [dbo].[webpages_Roles] (
    [RoleId] int IDENTITY(1,1) NOT NULL,
    [RoleName] nvarchar(256)  NOT NULL
);
GO

-- Creating table 'Video'
CREATE TABLE [dbo].[Video] (
    [ID] int IDENTITY(1,1) NOT NULL,
    [Title] nvarchar(max)  NOT NULL,
    [Recordingdate] datetime  NOT NULL,
    [SessionHash] nvarchar(max)  NOT NULL,
    [Filename] nvarchar(max)  NOT NULL,
    [Description] nvarchar(max)  NULL,
    [Groups_ID] int  NOT NULL,
    [Creator_UserId] int  NOT NULL,
    [Category_ID] int  NOT NULL,
    [Opponent_ID] int  NULL
);
GO

-- Creating table 'VideoViewcount'
CREATE TABLE [dbo].[VideoViewcount] (
    [ID] bigint IDENTITY(1,1) NOT NULL,
    [Viewtime] datetime  NOT NULL,
    [Video_ID] int  NOT NULL,
    [User_UserId] int  NOT NULL
);
GO

-- Creating table 'VideoComment'
CREATE TABLE [dbo].[VideoComment] (
    [ID] int IDENTITY(1,1) NOT NULL,
    [Content] nvarchar(600)  NOT NULL,
    [Created] datetime  NOT NULL,
    [Creator_UserId] int  NOT NULL,
    [Video_ID] int  NOT NULL
);
GO

-- Creating table 'Presence'
CREATE TABLE [dbo].[Presence] (
    [ID] int IDENTITY(1,1) NOT NULL,
    [Day] datetime  NOT NULL
);
GO

-- Creating table 'Groups'
CREATE TABLE [dbo].[Groups] (
    [ID] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Description] nvarchar(max)  NOT NULL,
    [Creator_UserId] int  NOT NULL
);
GO

-- Creating table 'File'
CREATE TABLE [dbo].[File] (
    [ID] int IDENTITY(1,1) NOT NULL,
    [Filename] nvarchar(max)  NOT NULL,
    [Created] datetime  NOT NULL,
    [Type] nvarchar(max)  NOT NULL,
    [Title] nvarchar(max)  NOT NULL,
    [Description] nvarchar(max)  NOT NULL,
    [Group_ID] int  NOT NULL,
    [Creator_UserId] int  NOT NULL,
    [Folder_ID] int  NOT NULL
);
GO

-- Creating table 'FileViewcount'
CREATE TABLE [dbo].[FileViewcount] (
    [ID] bigint IDENTITY(1,1) NOT NULL,
    [Viewtime] datetime  NOT NULL,
    [File_ID] int  NOT NULL,
    [User_UserId] int  NOT NULL
);
GO

-- Creating table 'FileFolder'
CREATE TABLE [dbo].[FileFolder] (
    [ID] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [IsPrivate] bit  NOT NULL,
    [Creator_UserId] int  NOT NULL
);
GO

-- Creating table 'FileComment'
CREATE TABLE [dbo].[FileComment] (
    [ID] int IDENTITY(1,1) NOT NULL,
    [Content] nvarchar(600)  NOT NULL,
    [Created] datetime  NOT NULL,
    [File_ID] int  NOT NULL,
    [Creator_UserId] int  NOT NULL
);
GO

-- Creating table 'VideoCategory'
CREATE TABLE [dbo].[VideoCategory] (
    [ID] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [IsPrivate] bit  NOT NULL,
    [Creator_UserId] int  NOT NULL
);
GO

-- Creating table 'Opponent'
CREATE TABLE [dbo].[Opponent] (
    [ID] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Creator_UserId] int  NOT NULL
);
GO

-- Creating table 'News'
CREATE TABLE [dbo].[News] (
    [ID] int IDENTITY(1,1) NOT NULL,
    [Title] nvarchar(max)  NOT NULL,
    [Content] nvarchar(max)  NOT NULL,
    [Created] nvarchar(max)  NOT NULL,
    [Creator_UserId] int  NOT NULL
);
GO

-- Creating table 'webpages_UsersInRoles'
CREATE TABLE [dbo].[webpages_UsersInRoles] (
    [webpages_Roles_RoleId] int  NOT NULL,
    [UserProfile_UserId] int  NOT NULL
);
GO

-- Creating table 'UserGroup'
CREATE TABLE [dbo].[UserGroup] (
    [Members_UserId] int  NOT NULL,
    [Groups_ID] int  NOT NULL
);
GO

-- Creating table 'AbsentUsers'
CREATE TABLE [dbo].[AbsentUsers] (
    [AbsentUsers_UserId] int  NOT NULL,
    [AbsentDays_ID] int  NOT NULL
);
GO

-- Creating table 'PresentUsers'
CREATE TABLE [dbo].[PresentUsers] (
    [PresentUsers_UserId] int  NOT NULL,
    [PresentDays_ID] int  NOT NULL
);
GO

-- Creating table 'ExcusedUsers'
CREATE TABLE [dbo].[ExcusedUsers] (
    [ExcusedUsers_UserId] int  NOT NULL,
    [ExcusedDays_ID] int  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [UserId] in table 'User'
ALTER TABLE [dbo].[User]
ADD CONSTRAINT [PK_User]
    PRIMARY KEY CLUSTERED ([UserId] ASC);
GO

-- Creating primary key on [UserId] in table 'webpages_Membership'
ALTER TABLE [dbo].[webpages_Membership]
ADD CONSTRAINT [PK_webpages_Membership]
    PRIMARY KEY CLUSTERED ([UserId] ASC);
GO

-- Creating primary key on [Provider], [ProviderUserId] in table 'webpages_OAuthMembership'
ALTER TABLE [dbo].[webpages_OAuthMembership]
ADD CONSTRAINT [PK_webpages_OAuthMembership]
    PRIMARY KEY CLUSTERED ([Provider], [ProviderUserId] ASC);
GO

-- Creating primary key on [RoleId] in table 'webpages_Roles'
ALTER TABLE [dbo].[webpages_Roles]
ADD CONSTRAINT [PK_webpages_Roles]
    PRIMARY KEY CLUSTERED ([RoleId] ASC);
GO

-- Creating primary key on [ID] in table 'Video'
ALTER TABLE [dbo].[Video]
ADD CONSTRAINT [PK_Video]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'VideoViewcount'
ALTER TABLE [dbo].[VideoViewcount]
ADD CONSTRAINT [PK_VideoViewcount]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'VideoComment'
ALTER TABLE [dbo].[VideoComment]
ADD CONSTRAINT [PK_VideoComment]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'Presence'
ALTER TABLE [dbo].[Presence]
ADD CONSTRAINT [PK_Presence]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'Groups'
ALTER TABLE [dbo].[Groups]
ADD CONSTRAINT [PK_Groups]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'File'
ALTER TABLE [dbo].[File]
ADD CONSTRAINT [PK_File]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'FileViewcount'
ALTER TABLE [dbo].[FileViewcount]
ADD CONSTRAINT [PK_FileViewcount]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'FileFolder'
ALTER TABLE [dbo].[FileFolder]
ADD CONSTRAINT [PK_FileFolder]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'FileComment'
ALTER TABLE [dbo].[FileComment]
ADD CONSTRAINT [PK_FileComment]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'VideoCategory'
ALTER TABLE [dbo].[VideoCategory]
ADD CONSTRAINT [PK_VideoCategory]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'Opponent'
ALTER TABLE [dbo].[Opponent]
ADD CONSTRAINT [PK_Opponent]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [ID] in table 'News'
ALTER TABLE [dbo].[News]
ADD CONSTRAINT [PK_News]
    PRIMARY KEY CLUSTERED ([ID] ASC);
GO

-- Creating primary key on [webpages_Roles_RoleId], [UserProfile_UserId] in table 'webpages_UsersInRoles'
ALTER TABLE [dbo].[webpages_UsersInRoles]
ADD CONSTRAINT [PK_webpages_UsersInRoles]
    PRIMARY KEY NONCLUSTERED ([webpages_Roles_RoleId], [UserProfile_UserId] ASC);
GO

-- Creating primary key on [Members_UserId], [Groups_ID] in table 'UserGroup'
ALTER TABLE [dbo].[UserGroup]
ADD CONSTRAINT [PK_UserGroup]
    PRIMARY KEY NONCLUSTERED ([Members_UserId], [Groups_ID] ASC);
GO

-- Creating primary key on [AbsentUsers_UserId], [AbsentDays_ID] in table 'AbsentUsers'
ALTER TABLE [dbo].[AbsentUsers]
ADD CONSTRAINT [PK_AbsentUsers]
    PRIMARY KEY NONCLUSTERED ([AbsentUsers_UserId], [AbsentDays_ID] ASC);
GO

-- Creating primary key on [PresentUsers_UserId], [PresentDays_ID] in table 'PresentUsers'
ALTER TABLE [dbo].[PresentUsers]
ADD CONSTRAINT [PK_PresentUsers]
    PRIMARY KEY NONCLUSTERED ([PresentUsers_UserId], [PresentDays_ID] ASC);
GO

-- Creating primary key on [ExcusedUsers_UserId], [ExcusedDays_ID] in table 'ExcusedUsers'
ALTER TABLE [dbo].[ExcusedUsers]
ADD CONSTRAINT [PK_ExcusedUsers]
    PRIMARY KEY NONCLUSTERED ([ExcusedUsers_UserId], [ExcusedDays_ID] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [webpages_Roles_RoleId] in table 'webpages_UsersInRoles'
ALTER TABLE [dbo].[webpages_UsersInRoles]
ADD CONSTRAINT [FK_webpages_UsersInRoles_webpages_Roles]
    FOREIGN KEY ([webpages_Roles_RoleId])
    REFERENCES [dbo].[webpages_Roles]
        ([RoleId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [UserProfile_UserId] in table 'webpages_UsersInRoles'
ALTER TABLE [dbo].[webpages_UsersInRoles]
ADD CONSTRAINT [FK_webpages_UsersInRoles_UserProfile]
    FOREIGN KEY ([UserProfile_UserId])
    REFERENCES [dbo].[User]
        ([UserId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_webpages_UsersInRoles_UserProfile'
CREATE INDEX [IX_FK_webpages_UsersInRoles_UserProfile]
ON [dbo].[webpages_UsersInRoles]
    ([UserProfile_UserId]);
GO

-- Creating foreign key on [Video_ID] in table 'VideoViewcount'
ALTER TABLE [dbo].[VideoViewcount]
ADD CONSTRAINT [FK_VideoViewcountVideo]
    FOREIGN KEY ([Video_ID])
    REFERENCES [dbo].[Video]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_VideoViewcountVideo'
CREATE INDEX [IX_FK_VideoViewcountVideo]
ON [dbo].[VideoViewcount]
    ([Video_ID]);
GO

-- Creating foreign key on [Groups_ID] in table 'Video'
ALTER TABLE [dbo].[Video]
ADD CONSTRAINT [FK_VideoGroup]
    FOREIGN KEY ([Groups_ID])
    REFERENCES [dbo].[Groups]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_VideoGroup'
CREATE INDEX [IX_FK_VideoGroup]
ON [dbo].[Video]
    ([Groups_ID]);
GO

-- Creating foreign key on [Creator_UserId] in table 'VideoComment'
ALTER TABLE [dbo].[VideoComment]
ADD CONSTRAINT [FK_CommentUser]
    FOREIGN KEY ([Creator_UserId])
    REFERENCES [dbo].[User]
        ([UserId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CommentUser'
CREATE INDEX [IX_FK_CommentUser]
ON [dbo].[VideoComment]
    ([Creator_UserId]);
GO

-- Creating foreign key on [User_UserId] in table 'VideoViewcount'
ALTER TABLE [dbo].[VideoViewcount]
ADD CONSTRAINT [FK_UserVideoViewcount]
    FOREIGN KEY ([User_UserId])
    REFERENCES [dbo].[User]
        ([UserId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserVideoViewcount'
CREATE INDEX [IX_FK_UserVideoViewcount]
ON [dbo].[VideoViewcount]
    ([User_UserId]);
GO

-- Creating foreign key on [Creator_UserId] in table 'Video'
ALTER TABLE [dbo].[Video]
ADD CONSTRAINT [FK_UserVideo]
    FOREIGN KEY ([Creator_UserId])
    REFERENCES [dbo].[User]
        ([UserId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserVideo'
CREATE INDEX [IX_FK_UserVideo]
ON [dbo].[Video]
    ([Creator_UserId]);
GO

-- Creating foreign key on [Members_UserId] in table 'UserGroup'
ALTER TABLE [dbo].[UserGroup]
ADD CONSTRAINT [FK_UserGroup_User]
    FOREIGN KEY ([Members_UserId])
    REFERENCES [dbo].[User]
        ([UserId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Groups_ID] in table 'UserGroup'
ALTER TABLE [dbo].[UserGroup]
ADD CONSTRAINT [FK_UserGroup_Group]
    FOREIGN KEY ([Groups_ID])
    REFERENCES [dbo].[Groups]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserGroup_Group'
CREATE INDEX [IX_FK_UserGroup_Group]
ON [dbo].[UserGroup]
    ([Groups_ID]);
GO

-- Creating foreign key on [Creator_UserId] in table 'Groups'
ALTER TABLE [dbo].[Groups]
ADD CONSTRAINT [FK_UserGroup1]
    FOREIGN KEY ([Creator_UserId])
    REFERENCES [dbo].[User]
        ([UserId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserGroup1'
CREATE INDEX [IX_FK_UserGroup1]
ON [dbo].[Groups]
    ([Creator_UserId]);
GO

-- Creating foreign key on [AbsentUsers_UserId] in table 'AbsentUsers'
ALTER TABLE [dbo].[AbsentUsers]
ADD CONSTRAINT [FK_AbsentUsers_User]
    FOREIGN KEY ([AbsentUsers_UserId])
    REFERENCES [dbo].[User]
        ([UserId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [AbsentDays_ID] in table 'AbsentUsers'
ALTER TABLE [dbo].[AbsentUsers]
ADD CONSTRAINT [FK_AbsentUsers_Presence]
    FOREIGN KEY ([AbsentDays_ID])
    REFERENCES [dbo].[Presence]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_AbsentUsers_Presence'
CREATE INDEX [IX_FK_AbsentUsers_Presence]
ON [dbo].[AbsentUsers]
    ([AbsentDays_ID]);
GO

-- Creating foreign key on [PresentUsers_UserId] in table 'PresentUsers'
ALTER TABLE [dbo].[PresentUsers]
ADD CONSTRAINT [FK_PresentUsers_User]
    FOREIGN KEY ([PresentUsers_UserId])
    REFERENCES [dbo].[User]
        ([UserId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [PresentDays_ID] in table 'PresentUsers'
ALTER TABLE [dbo].[PresentUsers]
ADD CONSTRAINT [FK_PresentUsers_Presence]
    FOREIGN KEY ([PresentDays_ID])
    REFERENCES [dbo].[Presence]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_PresentUsers_Presence'
CREATE INDEX [IX_FK_PresentUsers_Presence]
ON [dbo].[PresentUsers]
    ([PresentDays_ID]);
GO

-- Creating foreign key on [ExcusedUsers_UserId] in table 'ExcusedUsers'
ALTER TABLE [dbo].[ExcusedUsers]
ADD CONSTRAINT [FK_ExcusedUsers_User]
    FOREIGN KEY ([ExcusedUsers_UserId])
    REFERENCES [dbo].[User]
        ([UserId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [ExcusedDays_ID] in table 'ExcusedUsers'
ALTER TABLE [dbo].[ExcusedUsers]
ADD CONSTRAINT [FK_ExcusedUsers_Presence]
    FOREIGN KEY ([ExcusedDays_ID])
    REFERENCES [dbo].[Presence]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ExcusedUsers_Presence'
CREATE INDEX [IX_FK_ExcusedUsers_Presence]
ON [dbo].[ExcusedUsers]
    ([ExcusedDays_ID]);
GO

-- Creating foreign key on [Group_ID] in table 'File'
ALTER TABLE [dbo].[File]
ADD CONSTRAINT [FK_FileGroup]
    FOREIGN KEY ([Group_ID])
    REFERENCES [dbo].[Groups]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_FileGroup'
CREATE INDEX [IX_FK_FileGroup]
ON [dbo].[File]
    ([Group_ID]);
GO

-- Creating foreign key on [Creator_UserId] in table 'File'
ALTER TABLE [dbo].[File]
ADD CONSTRAINT [FK_FileUser]
    FOREIGN KEY ([Creator_UserId])
    REFERENCES [dbo].[User]
        ([UserId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_FileUser'
CREATE INDEX [IX_FK_FileUser]
ON [dbo].[File]
    ([Creator_UserId]);
GO

-- Creating foreign key on [File_ID] in table 'FileViewcount'
ALTER TABLE [dbo].[FileViewcount]
ADD CONSTRAINT [FK_FileViewcountFile]
    FOREIGN KEY ([File_ID])
    REFERENCES [dbo].[File]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_FileViewcountFile'
CREATE INDEX [IX_FK_FileViewcountFile]
ON [dbo].[FileViewcount]
    ([File_ID]);
GO

-- Creating foreign key on [User_UserId] in table 'FileViewcount'
ALTER TABLE [dbo].[FileViewcount]
ADD CONSTRAINT [FK_UserFileViewcount]
    FOREIGN KEY ([User_UserId])
    REFERENCES [dbo].[User]
        ([UserId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserFileViewcount'
CREATE INDEX [IX_FK_UserFileViewcount]
ON [dbo].[FileViewcount]
    ([User_UserId]);
GO

-- Creating foreign key on [Creator_UserId] in table 'FileFolder'
ALTER TABLE [dbo].[FileFolder]
ADD CONSTRAINT [FK_CategoryUser]
    FOREIGN KEY ([Creator_UserId])
    REFERENCES [dbo].[User]
        ([UserId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CategoryUser'
CREATE INDEX [IX_FK_CategoryUser]
ON [dbo].[FileFolder]
    ([Creator_UserId]);
GO

-- Creating foreign key on [Folder_ID] in table 'File'
ALTER TABLE [dbo].[File]
ADD CONSTRAINT [FK_FileCategoryFile]
    FOREIGN KEY ([Folder_ID])
    REFERENCES [dbo].[FileFolder]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_FileCategoryFile'
CREATE INDEX [IX_FK_FileCategoryFile]
ON [dbo].[File]
    ([Folder_ID]);
GO

-- Creating foreign key on [Video_ID] in table 'VideoComment'
ALTER TABLE [dbo].[VideoComment]
ADD CONSTRAINT [FK_VideoVideoComment]
    FOREIGN KEY ([Video_ID])
    REFERENCES [dbo].[Video]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_VideoVideoComment'
CREATE INDEX [IX_FK_VideoVideoComment]
ON [dbo].[VideoComment]
    ([Video_ID]);
GO

-- Creating foreign key on [File_ID] in table 'FileComment'
ALTER TABLE [dbo].[FileComment]
ADD CONSTRAINT [FK_FileFileComment]
    FOREIGN KEY ([File_ID])
    REFERENCES [dbo].[File]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_FileFileComment'
CREATE INDEX [IX_FK_FileFileComment]
ON [dbo].[FileComment]
    ([File_ID]);
GO

-- Creating foreign key on [Creator_UserId] in table 'FileComment'
ALTER TABLE [dbo].[FileComment]
ADD CONSTRAINT [FK_UserFileComment]
    FOREIGN KEY ([Creator_UserId])
    REFERENCES [dbo].[User]
        ([UserId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserFileComment'
CREATE INDEX [IX_FK_UserFileComment]
ON [dbo].[FileComment]
    ([Creator_UserId]);
GO

-- Creating foreign key on [Category_ID] in table 'Video'
ALTER TABLE [dbo].[Video]
ADD CONSTRAINT [FK_VideoCategoryVideo]
    FOREIGN KEY ([Category_ID])
    REFERENCES [dbo].[VideoCategory]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_VideoCategoryVideo'
CREATE INDEX [IX_FK_VideoCategoryVideo]
ON [dbo].[Video]
    ([Category_ID]);
GO

-- Creating foreign key on [Creator_UserId] in table 'VideoCategory'
ALTER TABLE [dbo].[VideoCategory]
ADD CONSTRAINT [FK_VideoCategoryUser]
    FOREIGN KEY ([Creator_UserId])
    REFERENCES [dbo].[User]
        ([UserId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_VideoCategoryUser'
CREATE INDEX [IX_FK_VideoCategoryUser]
ON [dbo].[VideoCategory]
    ([Creator_UserId]);
GO

-- Creating foreign key on [Opponent_ID] in table 'Video'
ALTER TABLE [dbo].[Video]
ADD CONSTRAINT [FK_OpponentVideo]
    FOREIGN KEY ([Opponent_ID])
    REFERENCES [dbo].[Opponent]
        ([ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_OpponentVideo'
CREATE INDEX [IX_FK_OpponentVideo]
ON [dbo].[Video]
    ([Opponent_ID]);
GO

-- Creating foreign key on [Creator_UserId] in table 'Opponent'
ALTER TABLE [dbo].[Opponent]
ADD CONSTRAINT [FK_OpponentUser]
    FOREIGN KEY ([Creator_UserId])
    REFERENCES [dbo].[User]
        ([UserId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_OpponentUser'
CREATE INDEX [IX_FK_OpponentUser]
ON [dbo].[Opponent]
    ([Creator_UserId]);
GO

-- Creating foreign key on [Creator_UserId] in table 'News'
ALTER TABLE [dbo].[News]
ADD CONSTRAINT [FK_NewsUser]
    FOREIGN KEY ([Creator_UserId])
    REFERENCES [dbo].[User]
        ([UserId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_NewsUser'
CREATE INDEX [IX_FK_NewsUser]
ON [dbo].[News]
    ([Creator_UserId]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------

这是我的 _AppStart.cshtml

@using Coding.Lizards.Video.Manager.Web.Models;
@{
    WebSecurity.InitializeDatabaseConnection("DefaultConnection", "User", "UserId", "Emailaddress", autoCreateTables: true);
    WebsiteModel model = new WebsiteModel();
    if (!Roles.GetAllRoles().Contains("addgroup")) {
        Roles.CreateRole("addgroup");
    }
}

有任何想法吗?

4

2 回答 2

2

尝试将 pages_UsersInRoles 的创建脚本更改为:

CREATE TABLE [dbo].[webpages_UsersInRoles] (
    [RoleId] int  NOT NULL,
    [UserId] int  NOT NULL
);

如果您有兴趣,我使用反射来查看抛出异常的方法,它看起来像这样:

internal static string UsersInRoleTableName
{
  get
  {
    return "webpages_UsersInRoles";
  }
}


List<object> list = Enumerable.ToList<object>(database.Query("SELECT u." + this.SafeUserNameColumn + " FROM " + this.SafeUserTableName + " u, " + SimpleRoleProvider.UsersInRoleTableName + " ur, " + SimpleRoleProvider.RoleTableName + " r Where (r.RoleName = @0 and ur.RoleId = r.RoleId and ur.UserId = u." + this.SafeUserIdColumn + " and u." + this.SafeUserNameColumn + " LIKE @1)", (object) roleName, (object) usernameToMatch));

然后,您应该能够通过从数据库中刷新模型或直接在设计器中编辑属性名称来更改 Entity Framework 模型中的属性名称。在 VS2010 中,您可以单击属性,然后再次单击以编辑它(就像在资源管理器中更改文件名一样),或者在属性窗格中更改名称。

于 2013-09-10T19:46:19.880 回答
1

我有同样的问题。我通过在我的项目中启用迁移然后在以下步骤之后执行我的项目来解决它。

  1. 删除项目的数据库
  2. 转到工具 -> 库包管理器 -> 包管理器控制台
  3. 下午>Enable-Migrations
  4. 下午>Update-Database

如果您在项目的 Web.Config 中配置连接字符串,则将创建数据库以及包含所需列的表。

于 2014-01-02T14:23:45.420 回答