Reverse Engineer Code First - Generates a context file with error - c#

Got the following Database:
GO
/****** Object: Table [dbo].[Emp] Script Date: 2/25/2013 09:52:26 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Emp](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](max) NOT NULL,
[Age] [int] NOT NULL,
[DateOfBirth] [date] NOT NULL,
CONSTRAINT [PK_Emp] 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] TEXTIMAGE_ON [PRIMARY]
GO
When i try to do Entity Framework -> reverse engineer code first on an empty project in Visual Studio 2012 c# i get the following error in the DBContext as soon as it is done
Error 1 An object reference is required for the non-static field, method, or property 'System.Data.Entity.DbContext.Database.get'
why might that be?
EDIT:
Worked in an empty project
doesn't work on a non empty project

"Database" is a commonly used class/namespace/variable name, so it's getting confused as to which one you mean. Notice that the error mentioned "System.Data.Entity.DbContext.Database". Just qualify it with "System.Data.Entity" to get the correct one:
System.Data.Entity.Database.SetInitializer(null);

Related

Unable to track an instance of type ** because it does not have a primary key. Only entity types with primary keys may be tracked

Having an issue with a specific table on my project which is driving me up the wall.
I am trying to delete all the entries where from a table where the eventUId is something.
so my code is this
var SessionsToRemove = _context.Connlog.Where(s => s.Eventuid == EventDetails.Uid);
_context.Connlog.RemoveRange(SessionsToRemove);
_context.SaveChanges();
My table on SQL server has a KEY which is defined to the column ID.
This works on all the other tables, except for this one.
Can someone please help me?
#Caius
USE [vents]
GO
/****** Object: Table [dbo].[Connlog] Script Date: 17/02/2021 12:46:49 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Connlog](
[id] [int] IDENTITY(1,1) NOT NULL,
[uid] [nvarchar](max) NULL,
[chatconnid] [nvarchar](100) NULL,
[connstart] [datetime] NULL,
[connend] [datetime] NULL,
[useremailaddress] [nvarchar](500) NULL,
[eventuid] [nvarchar](500) NULL,
CONSTRAINT [PK_Connlog] 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] TEXTIMAGE_ON [PRIMARY]
GO
EDIT:
After the last edit, I've decided to scaffold the database again which sorted the the issue.
Thank you guys for your help.

Are SQL Server computed primary key columns supported in Entity Framework 4? Exception thrown when inserting record

Are Sql Server computed primary key columns supported when inserting/updating records with Entity Framework 4.0? I know they're not in Linq to SQL.
Every time I try to insert a record that has a computed primary key column defined in Sql Server, I get the error
"Modifications to tables where a primary key column has property 'StoreGeneratedPattern' set to 'Computed' are not supported. Use 'Identity' pattern instead. "
even when the key's StoreGeneratedPattern property is set to Identity or Computed.
Example Table:
CREATE TABLE [dbo].[Table](
[B1] [varchar](10) NOT NULL,
[L1] [varchar](5) NOT NULL,
[L2] [varchar](5) NULL,
[E1] [varchar](7) NOT NULL,
[D1] [varchar](50) NULL,
[R1] [varchar](max) NULL,
[Z1] [varchar](5) NULL,
[D2] [varchar](5) NULL,
[Key] AS (([E1]+[B1])+[L1]),
CONSTRAINT [PK_Table_1] PRIMARY KEY CLUSTERED
(
[Key] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
Example edmx :
EDMX with Key properties displayed
I would've just set up a normal auto-increment ID column in the database, but I've inherited this project and have been told to leave the database structure alone.

SSIS Parent table Child relation migration

I'm trying to use SSIS to move some data from one SQL server to my Destimation SQL server, the source has a table "Parent" with Identity field ID that is a Foreign key to the "Child" table.
1 - N relation
The question is simple, what is the best way to transfer the data to a different SQL Server with still a parent child relation.
Note: Both ID (Parent and Child) are identity fields that we do not want to migrate since the destination source wont necessary need to have them.
Please share your comments and ideas.
FYI: We create a .Net Code (C#) that does this, we have a query that gets parent data, a query that get childs data and using linq we join the data and we loop parent getting the new ID and inserting as reference of second table. This is working but we want to create the same on SSIS to be able to scale later.
You have to import Parent Table Before Child Table:
First You have to Create Tables On Destination Server, you can achieve this using an query like the following:
CREATE TABLE [dbo].[Tbl_Child](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Parent_ID] [int] NULL,
[Name] [varchar](50) NULL,
CONSTRAINT [PK_Tbl_Child] 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]
GO
CREATE TABLE [dbo].[Tbl_Parent](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](50) NULL,
CONSTRAINT [PK_Tbl_Parent] 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]
GO
ALTER TABLE [dbo].[Tbl_Child] WITH CHECK ADD CONSTRAINT [FK_Tbl_Child_Tbl_Parent] FOREIGN KEY([Parent_ID])
REFERENCES [dbo].[Tbl_Parent] ([ID])
GO
ALTER TABLE [dbo].[Tbl_Child] CHECK CONSTRAINT [FK_Tbl_Child_Tbl_Parent]
GO
Add two OLEDB Connection manager (Source & Destination)
Next you have to add a DataFlow Task to Import Parent Table Data From Source. You have to check Keep Identity option
Next you have to add a DataFlow Task to Import Child Table Data From Source. You have to check Keep Identity option
Package May Look like the following
WorkAround: you can disable constraint and import data then enabling it by adding a SQL Task before and after Importing
Disable Constraint:
ALTER TABLE Tbl_Child NOCHECK CONSTRAINT FK_Tbl_Child_Tbl_Parent
Enable Constraint:
ALTER TABLE Tbl_Child CHECK CONSTRAINT FK_Tbl_Child_Tbl_Parent
if using this Workaround it is not necessary to follow an order when importing

Entity Framework Keeps overwriting attributes. Need other way of solving issue

I have an existing app / dataabse. I have been tasked to add in Entity Framework as part of an upgrade.
I hit a problem where when I generate (or regenerate) the edmx, the code no longer recognises the foreign keys in the database tables, and when the code runs, it complains about missing id's, as, I assume, it is 'guessing' what the foreign keys should be.
I can get round this by adding the following attribute to the Auto generated model definitions.
[ForeignKey("NavigationProperty")]
But then, if / when the edmx is regenerated, all this gets blown away, and has to be re-added.
Although the class that is generated is partial, as these attributes are being added to existing members, I cannot move them to a seperate file.
So, how do I get round this option? Ideally I'd like to ensure that when the edmx is generated it picks up the foreign keys, so that this issue is fixed permanently. If that can't be done, next step is to ask if there is some way of programatically generating these associations, so it is only done once.
Thanks
edit - Added in sample table definition
Here is the code auto generated by SMS. Is tehre anything wrong with the foreign key definition?
CREATE TABLE [dbo].[ShopProductTypes](
[id] [int] IDENTITY(1,1) NOT NULL,
[Shop_Id] [int] NOT NULL,
[Product_Id] [int] NOT NULL,
[CreatedDate] [datetime] NOT NULL,
[CancelledDate] [datetime] NULL,
[Archived] [bit] NOT NULL,
CONSTRAINT [PK_ShopProductTypes] 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]
GO
ALTER TABLE [dbo].[ShopProductTypes] WITH CHECK ADD CONSTRAINT [FK_ShopProductTypes_Shop] FOREIGN KEY([Shop_Id])
REFERENCES [dbo].[Shops] ([Id])
GO
I found this:
http://blogs.msdn.com/b/dsimmons/archive/2007/09/01/ef-codegen-events-for-fun-and-profit-aka-how-to-add-custom-attributes-to-my-generated-classes.aspx
It's a bit more involved.

execute sql query from c# code for creating new sql table

I am trying to execute a sql query which will create a new table in sql database. When I am doing this:
string queryString = #"
CREATE TABLE [dbo].[peep_searchresults_live](
[id] [int] IDENTITY(1,1) NOT NULL,
[SKU] [varchar](150) NULL,
[ManufacturerBrand] [varchar](150) NULL,
);
The query gets executed just fine and the table gets created in the database.
But when I am trying this:
string queryString = #"
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[peep_searchresults_live](
[id] [int] IDENTITY(1,1) NOT NULL,
[SKU] [varchar](150) NULL,
[ManufacturerBrand] [varchar](150) NULL,
CONSTRAINT [PK_peep_searchresults_live] 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]
GO
SET ANSI_PADDING OFF
GO";
I am receiving error like this one:
SQL server doesn't understand what GO is - only the query analyzer knows (eg. SSMS).
If you need to execute multiple commands, simply execute them in the proper order, one at a time.
GO statements are not valid in ADO.net
Do you need all of those other qualifying statements?
You can break each into a separate command, and execute each command in turn with a single connection.
You cannot use go, you need to split the command in to multiple sets.
http://social.msdn.microsoft.com/Forums/ar/csharpgeneral/thread/2907541f-f1cf-40ea-8291-771734de55f2

Categories

Resources