How to create simple membership sql tables manually? - asp.net-mvc

I'm looking for a tool similar to Aspnet_regsql.exe for ASP .NET Membership just for simpleMemberShip Provider. Creating the tables at runtime is too late, cause a client program uses the same tables.
UserProfile
webpages_Membership
webpages_OAuthMembership
webpages_Roles
webpages_UsersInRoles
Any advice would be great

You could just create the script yourself, using SQL Management Studio, after running your application on your developer machine:
Example follows from a similar task I did (note dates, not sure if anything has changed).
Note: you'll need to create the database and do the normal permission setup etc as you would do for any production system.
/****** Object: Table [dbo].[UserProfile] Script Date: 12/06/2012 18:18:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[UserProfile](
[UserId] [int] IDENTITY(1,1) NOT NULL,
[UserName] [nvarchar](56) NOT NULL,
PRIMARY KEY CLUSTERED
(
[UserId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
UNIQUE NONCLUSTERED
(
[UserName] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[webpages_Membership] Script Date: 12/06/2012 18:18:52 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
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,
PRIMARY KEY CLUSTERED
(
[UserId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[webpages_Membership] ADD DEFAULT ((0)) FOR [IsConfirmed]
GO
ALTER TABLE [dbo].[webpages_Membership] ADD DEFAULT ((0)) FOR [PasswordFailuresSinceLastSuccess]
GO
/****** Object: Table [dbo].[webpages_OAuthMembership] Script Date: 12/06/2012 18:19:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[webpages_OAuthMembership](
[Provider] [nvarchar](30) NOT NULL,
[ProviderUserId] [nvarchar](100) NOT NULL,
[UserId] [int] NOT NULL,
PRIMARY KEY CLUSTERED
(
[Provider] ASC,
[ProviderUserId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[webpages_Roles] Script Date: 12/06/2012 18:19:26 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[webpages_Roles](
[RoleId] [int] IDENTITY(1,1) NOT NULL,
[RoleName] [nvarchar](256) NOT NULL,
PRIMARY KEY CLUSTERED
(
[RoleId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
UNIQUE NONCLUSTERED
(
[RoleName] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[webpages_UsersInRoles] Script Date: 12/06/2012 18:19:39 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[webpages_UsersInRoles](
[UserId] [int] NOT NULL,
[RoleId] [int] NOT NULL,
PRIMARY KEY CLUSTERED
(
[UserId] ASC,
[RoleId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[webpages_UsersInRoles] WITH CHECK ADD CONSTRAINT [fk_RoleId] FOREIGN KEY([RoleId])
REFERENCES [dbo].[webpages_Roles] ([RoleId])
GO
ALTER TABLE [dbo].[webpages_UsersInRoles] CHECK CONSTRAINT [fk_RoleId]
GO
ALTER TABLE [dbo].[webpages_UsersInRoles] WITH CHECK ADD CONSTRAINT [fk_UserId] FOREIGN KEY([UserId])
REFERENCES [dbo].[UserProfile] ([UserId])
GO
ALTER TABLE [dbo].[webpages_UsersInRoles] CHECK CONSTRAINT [fk_UserId]
GO

Related

Linq2DB: Bulk copy failing for float type column in MS Sql server

I am trying to perform bulk copy for into a table that has few fields that are of type "float" in MS SQL Server. I noticed that after bulk copy completed, all these "Float" fields had value of "NULL". These fields allowed null value so these NULLs were there.
I traced the call in SQL Profiler and noticed that "INSERT BULK" command did not have these "float" fields.
I simplified my case to just simple object as below.
public class TestIt
{
public int Id{get;set;}
public string Title { get; set; } = $"Title{DateTime.UtcNow}";
public double? Price { get; set; }
}
The table has following schema.
CREATE TABLE [dbo].[TestIt](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Title] [nvarchar](50) NULL,
[Price] [float] NULL,
CONSTRAINT [PK_TestIt] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
When I execute BulkCopy for a list of these object, I can see that "Title" field was copied correctly. Where was "Price" field always have NULL.
Is this some limitation in Linq2Db or I am doing something wrong.
When I use bulk copy with EF, it works fine.

Entity Framework 6 - SQL Server and Oracle (SaveChanges)

I have an application which uses below (Employee) domain model for SQL Server and Oracle data provider. This model works fine for Oracle data provider, however the same gives error for SQL Server. And when I change the field "ID" from decimal to Int it works fine for SQL Server but gives error in Oracle.
On application configuration I am changing the data provider from i.e. from SQL Server to Oracle or vice-versa.
Cannot insert explicit value for identity column in table 'Employee' when IDENTITY_INSERT is set to OFF.*
ORA-01400: cannot insert NULL into ("Emp"."Employee"."ID")
public class Employee
{
[Key, Column(Order = 0)]
public decimal ID { get; set; }
public string NAME { get; set; }
}
if (ID == null || ID == 0)
{
Model.Employee obj = new Model.Employee();
obj.NAME = Name;
if (Convert.ToInt32(ConfigurationSettings.AppSettings["DataProviderType"]) == "Oracle")
obj.ID = GetNextId();
DbContext.Employees.Add(obj);
DbContext.SaveChanges();
}
SQL Server table script
CREATE TABLE [dbo].[Employee](
[ID] [int] IDENTITY(1,1) NOT NULL,
[NAME] [varchar](256) NULL,
CONSTRAINT [Employee_PK] PRIMARY KEY CLUSTERED
(
[ID] ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
Oracle table script
CREATE TABLE Employee
(
"ID" NUMBER NOT NULL ENABLE,
"NAME" VARCHAR2(256)
);
CREATE UNIQUE INDEX "PK_Employee" ON "Employee" ("ID");
ALTER TABLE "Employee" ADD CONSTRAINT "Employee_PK" PRIMARY KEY ("ID") ENABLE;
How can I handle this in my application i.e. in DOTNET Side?
With suggestion from #IvanStoev, I have change datatype from int to numeric IDENTITY(1,1) NOT NULL. in sql server. And also handled IDENTITY_INSERT error with below code,
modelBuilder.Entity<Employee>().Property(i => i.ID).HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity);
Hope this will help someone with similar issue.

EF6.1.3 Join columns (like in hibernate)

I'm using Entitiy Framework 6.1.3 and I want to add a relation from N tables to 1 table with more than 1 join columns. The following example will help you to understand my problem:
Table P (protocol table) with these columns:
protocol_id (int, PK)
message (string)
module (string)
reference_id (int)
protocol_id | module | message | reference_id
-----------------------------------
1 | A | created | 1
2 | A | modified | 1
2 | A | created | 2
3 | B | deleted | 1
4 | B | modified | 1
Table A (module A table) with these columns:
mod_id (int, PK)
mod_x (string)
mod_y (...)
...
mod_id | mod_x | ...
--------------------
1 | abc | ...
2 | xyz | ...
Table B (module B table) with these columns:
mod_id (int, PK)
mod_x (string)
mod_y (...)
...
mod_id | mod_x | ...
--------------------
1 | abc | ...
2 | xyz | ...
I would like to have a navigation property in module A to get all procotols for this row - sthg like that:
where module = 'A' and reference_id = mod_id
(same for module B, module C ...)
In Java/Hibernate, I know you can use more than one ElementJoinColumns (with referenceColumnsNames, ...).
How can I handle this with EF6.1?
Thanks, Markus.
Here is the DDL. It is important that you have the proper referential integrity (RI) set up:
USE [Breaz]
GO
/****** Object: Table [dbo].[TableA] Script Date: 9/14/2016 3:25:43 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[TableA](
[mod_id] [int] IDENTITY(1,1) NOT NULL,
[mod_x] [varchar](10) NULL,
CONSTRAINT [PK_TableA_] PRIMARY KEY CLUSTERED
(
[mod_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[TableB] Script Date: 9/14/2016 3:25:43 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[TableB](
[mod_id] [int] IDENTITY(1,1) NOT NULL,
[mod_x] [varchar](10) NULL,
CONSTRAINT [PK_tableb] PRIMARY KEY CLUSTERED
(
[mod_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[TableP] Script Date: 9/14/2016 3:25:43 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[TableP](
[protocol_id] [int] IDENTITY(1,1) NOT NULL,
[message] [varchar](10) NULL,
[module] [varchar](10) NULL,
[reference_id] [int] NULL,
CONSTRAINT [PK_TableP] PRIMARY KEY CLUSTERED
(
[protocol_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[TableP] WITH CHECK ADD CONSTRAINT [FK_TableP_tablea] FOREIGN KEY([reference_id])
REFERENCES [dbo].[TableA] ([mod_id])
GO
ALTER TABLE [dbo].[TableP] CHECK CONSTRAINT [FK_TableP_tablea]
GO
ALTER TABLE [dbo].[TableP] WITH CHECK ADD CONSTRAINT [FK_TableP_tableb] FOREIGN KEY([reference_id])
REFERENCES [dbo].[TableB] ([mod_id])
GO
ALTER TABLE [dbo].[TableP] CHECK CONSTRAINT [FK_TableP_tableb]
GO
Now add the edmx for the three tables, and your linq should look like this (use your dbcontext instead of BreazEntities22 :
var tableA = e.TableAs.
Where(w => w.TablePs.Any(s => s.module == "A")).ToList();
var tableB = e.TableBs.
Where(w => w.TablePs.Any(s => s.module == "A")).ToList();

Insert data in SQL Server stored procedure

I am trying to add data in single table using a SQL Server stored procedure and I am getting an error
procedure or function x has too many argument specified
Also how can I get newly created ID of record as I need to update multiple tables using these IDs
I am using SQL Server 2012
Many thanks
ALTER PROCEDURE [dbo].[CreateNewFunctionsNavigation]
#FunctionName nvarchar(250),
#Hierarchy_Level int
AS
BEGIN
SET NOCOUNT ON;
INSERT [dbo].[Navigation_Functions] ([FunctionName], [Hierarchy_Level])
VALUES(#FunctionName, #Hierarchy_Level)
END
Execution of stored procedure:
DECLARE #return_value int
EXEC #return_value = [dbo].[CreateNewFunctionsNavigation]
#FunctionName = N'DSD',
#Hierarchy_Level = 3
SELECT 'Return Value' = #return_value
GO
My SQL for Function table
USE [MySolution01_DB]
GO
/****** Object: Table [dbo].[Navigation_Functions] Script Date: 06/01/2015 15:58:57 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Navigation_Functions](
[Function_ID] [int] IDENTITY(1,1) NOT NULL,
[FunctionName] [nvarchar](250) NOT NULL,
[Hierarchy_Level] [int] NOT NULL,
CONSTRAINT [PK_Functions] PRIMARY KEY CLUSTERED
(
[Function_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
It should be
INSERT INTO [dbo].[Navigation_Functions] ([FunctionName], [Hierarchy_Level])
I created a sample table
CREATE TABLE [dbo].[Navigation_Functions](
[NavigationFunctionId] [int] IDENTITY(1,1) NOT NULL,
[FunctionName] [nvarchar](250) NULL,
[Hierarchy_Level] [int] NULL,
CONSTRAINT [PK_Navigation_Functions] PRIMARY KEY CLUSTERED
(
[NavigationFunctionId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
You can get the last ID by: SELECT SCOPE_IDENTITY();
USE [MySolution01_DB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[CreateNewFunctionsNavigation]
#FunctionName nvarchar(250),
#Hierarchy_Level INT,
#Function_identity INT OUTPUT
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO [dbo].[Navigation_Functions] ([FunctionName],[Hierarchy_Level] )
VALUES(#FunctionName, #Hierarchy_Level);
SET #Function_identity=SCOPE_IDENTITY()
RETURN #Function_identity
RETURN
END

MVC Entity Framework Model not returning correct data

Run into a strange problem while writing an ASP.NET MVC site. I have a view in my SQL Server database that returns a few date ranges. The view works fine when running the query in SSMS.
When the view data is returned by the Entity Framework Model, It returns the correct number of rows but some of the rows are duplicated.
Here is an example of what I have done:
SQL Server code:
EDITED: (table A)
CREATE TABLE [dbo].[A](
[ID] [int] NOT NULL,
[PhID] [int] NULL,
[FromDate] [datetime] NOT NULL,
[ToDate] [datetime] NULL,
CONSTRAINT [PK_A] PRIMARY KEY CLUSTERED
( [ID] ASC,
[FromDate] ASC
)) ON [PRIMARY]
CREATE TABLE [dbo].[B](
[PhID] [int] NOT NULL,
[FromDate] [datetime] NULL,
[ToDate] [datetime] NULL,
CONSTRAINT [PK_B] PRIMARY KEY CLUSTERED
( [PhID] ASC )) ON [PRIMARY]
go
CREATE VIEW C as
SELECT A.ID,
CASE WHEN A.PhID IS NULL THEN A.FromDate ELSE B.FromDate END AS FromDate,
CASE WHEN A.PhID IS NULL THEN A.ToDate ELSE B.ToDate END AS ToDate
FROM A
LEFT OUTER JOIN B ON A.PhID = B.PhID
go
INSERT INTO B (PhID, FromDate, ToDate) VALUES (100, '20100615', '20100715')
INSERT INTO A (ID, PhID, FromDate, ToDate) VALUES (1, NULL, '20100101', '20100201')
INSERT INTO A (ID, PhID, FromDate, ToDate) VALUES (1, 100, '20100615', '20100715')
INSERT INTO B (PhID, FromDate, ToDate) VALUES (101, '20101201', '20101231')
INSERT INTO A (ID, PhID, FromDate, ToDate) VALUES (2, NULL, '20100801', '20100901')
INSERT INTO A (ID, PhID, FromDate, ToDate) VALUES (2, 101, '20101201', '20101231')
So now, if you select all from C, you get 4 separate date ranges
In the Entity Framework Model (which I call 'Core'), the view 'C' is added.
in MVC Controller:
public class HomeController : Controller
{
public ActionResult Index()
{
CoreEntities db = new CoreEntities();
var clist = from c in db.C
select c;
return View(clist.ToList());
}
}
in MVC View:
#model List<RM.Models.C>
#{
foreach (RM.Models.C c in Model)
{
#String.Format("{0:dd-MMM-yyyy}", c.FromDate)
<span>-</span>
#String.Format("{0:dd-MMM-yyyy}", c.ToDate)
<br />
}
}
When I run all this, it outputs this:
01-Jan-2010 - 01-Feb-2010
01-Jan-2010 - 01-Feb-2010
01-Aug-2010 - 01-Sep-2010
01-Aug-2010 - 01-Sep-2010
When it should do this (this is what the view returns):
01-Jan-2010 - 01-Feb-2010
15-Jun-2010 - 15-Jul-2010
01-Aug-2010 - 01-Sep-2010
01-Dec-2010 - 31-Dec-2010
Also, I've run the SQL profiler over it and according to that, the query being executed is:
SELECT
[Extent1].[ID] AS [ID],
[Extent1].[FromDate] AS [FromDate],
[Extent1].[ToDate] AS [ToDate]
FROM (SELECT
[C].[ID] AS [ID],
[C].[FromDate] AS [FromDate],
[C].[ToDate] AS [ToDate]
FROM [dbo].[C] AS [C]) AS [Extent1]
Which returns the correct data
So it seems that the entity framework is doing something to the data in the meantime.
To me, everything looks fine! Have I missed something?
Cheers,
Ben
EDIT:
sorry, table A should be:
CREATE TABLE [dbo].[A](
[ID] [int] NOT NULL,
[PhID] [int] NULL,
[FromDate] [datetime] NOT NULL,
[ToDate] [datetime] NULL,
CONSTRAINT [PK_A] PRIMARY KEY CLUSTERED
( [ID] ASC,
[FromDate] ASC
)) ON [PRIMARY]
I figured it out myself.
The problem was with the way the view was mapped in the entity model.
When it was added, it made the entity key just the ID. I needed it over the ID and FromDate. So I included the FromDate in the entity key and it works fine.

Resources