Does SQL Server Compact support brackets? - ado.net

According to this http://technet.microsoft.com/en-us/library/ms174147.aspx SQL Server Compact 4.0 doesn't support brackets. Looking at EF5 migration SQL commands (using -verbose in the package manager console) I get code like the following:
CREATE TABLE [Products] (
[Id] [int] NOT NULL,
[Name] [nvarchar](4000),
CONSTRAINT [PK_dbo.Products] PRIMARY KEY ([Id])
)
Am I missing something here? Perhaps ADO.NET has some special handling?

SQL Server Compact supports brackets for identifiers, seems like a documentation issue.

Related

Migrating from SQL Server to Aurora PostgreSQL where encountering GUID, VARCHAR, UUID issues

I'm seeking some advice.
I've migrated a database from SQL Server to Aurora PostgreSQL using AWS DMS. In most of the tables in SQL Server, the primary keys are a uniqueidentifier (GUID). When migrated to Postgres these columns are converted to VARCHAR(36). This seems to be as expected, per the AWS DMS documentation.
In our .NET application, we use Entity Framework 6, which I have added a new dbContext to use the npgsql provider. Note that we are still keeping existing SQL Server EF6 providers. Essentially, the application will use both SQL Server and PostgreSQL. This is all hooked up fine.
Where I run into some issues is when my Postgres context is making fetches to the PostgreSQL database, it encounters a lot of errors
Npgsql.PostgresException: 42883: operator does not exist: character varying = uuid
I understand the issue, where the application using EF makes a fetch by Id (GUID), and the Postgres table has an Id that is VARCHAR type...
My feeling is the problem is not on the application or EF side, rather the column on the table should be something like a UUID. Which I can do, on post migration, I can simply alter the column to become a UUID type, but is this the way, and will it resolve my issues? I also feel like this can't be a unique case I'm dealing with; seems like a common issue for anyone also migrating a .NET app from SQL Server to PostgreSQL...
I look forward to hearing some of your ideas, comments, thoughts on this. Thanks in advance.
It seems that this migration procedure is not quite up to the task, as a GUID (which is Microsoft's confusing term for UUID) should be migrated to uuid. Not only would you save 21 bytes of storage space per row, but you also wouldn't have this problem.
It seems that your application is comparing a uuid value with one of the migrated varchars:
WHERE uniqueidentifier = UUID '87595807-3157-4a81-ac89-3e09e83c0c0a'
You have to add an explicit cast, like the error message says:
WHERE uniqueidentifier = CAST (UUID '87595807-3157-4a81-ac89-3e09e83c0c0a' AS text)
You would cast to text, not to varchar, because there is no equality operator for varchar. varchar is coerced to text when you compare it, because the storage for these types is identical.

EFCore, Is it possible to generate migration script without startup project?

I have googled a lot, but didn't find any useful information about the question.
Currently, I'm making a plugin system, each plugin is a .Net Core 2.2 ClassLibrary project.
In one plugin, it's going to create a new DbContext. To be able to deploy to production environment, I need to create migration script. But I just can't call Add-Migration or Script-Migration without the startup project.
Finally, We come up with a dummy startup project just for generating the script. However, the script doesn't include any connection info:
IF OBJECT_ID(N'[__EFMigrationsHistory]') IS NULL
BEGIN
CREATE TABLE [__EFMigrationsHistory] (
[MigrationId] nvarchar(150) NOT NULL,
[ProductVersion] nvarchar(32) NOT NULL,
CONSTRAINT [PK___EFMigrationsHistory] PRIMARY KEY ([MigrationId])
);
END;
GO
CREATE TABLE [Blogs] (
[BlogId] int NOT NULL IDENTITY,
[Url] nvarchar(max) NULL,
CONSTRAINT [PK_Blogs] PRIMARY KEY ([BlogId])
);
-- omit some similar code
CREATE INDEX [IX_Posts_BlogId] ON [Posts] ([BlogId]);
GO
INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'20190318085448_InitialCreate', N'2.2.3-servicing-35854');
GO
I don't understand the point to have a startup project. Is there any workaround? Thanks.

Existing Azure Database with Entity Framework 6 Alpha 2

I have a database on azure where clustered indices are required. I would like to use Entity-Framework 6 Alpha 2, because I would like to use the new async features. When I test it on my local machine with SQL Express 2012 everything is fine, but when I try it with my azure database I get the following error:
Tables without a clustered index are not supported in this version of SQL Server. Please create a clustered index and try again.
I have no idea what to do, because when I test it with an empty database every primary key is a clustered index.
Any ideas?
Would you add a bit clarification on your situation with "Existing Database on Windows Azure" and using with EF 6? First of all - are you using EF CodeFirst, ModelFirst, DatabaseFirst?
Then if you really have existing database, how did you create it? DB + Schema manually, using some wizard (SSMS, SQL Azure Migration Wizard, EF CodeFirst created it, etc?). How this existing DB ended being in Azure?
Then trace down the full error message, check for which table it happens and manually add the clustered index on that table. It is true that every primary key you create in SQL Server, by default is also a CLUSTERED. But if the table was created first, then the Primary Key was added as separate DDL statement (ALTER TABLE ....) it might not have been created as CLUSTERED.
So, the message is pretty clear - please create clustered index first. Find out which table is SQL Azure complaining about and create a clustered index on it. If it hasn't Primary Key, just add one as CLUSTERD:
ALTER TABLE [dbo].[Individual]
ADD CONSTRAINT [PK_Individual_CustomerID]
PRIMARY KEY CLUSTERED
(
[CustomerID] ASC
)
If it has a primary key - check which columns are included, drop it, and recreate it as clustered.

How to insert into a table with just one IDENTITY column (SQL Server CE)

I am trying to insert a value in a one IDENTITY column Table in SQL Server CE 3.5. I Tried the following:
INSERT Target DEFAULT VALUES
INSERT Target (ID) VALUES (DEFAULT)
INSERT Target (ID) VALUES ()
But none of them worked. This is the SQL command I used to create the table (Using SQL Server Management Studio):
CREATE TABLE Target(
ID int NOT NULL IDENTITY (1, 1) PRIMARY KEY
);
Microsoft help site (http://msdn.microsoft.com/en-us/library/ms174633%28SQL.90%29.aspx) mentions that DEFAULT values are not valid for identity columns however they do not mention any alternative.
They mention something about uniqueidentifier and ROWGUID but I have not been able to make it work.
I would appreciate any pointers on how to solve this problem or links to documentation about valid sql commands for sql server CE.
Thank you
Using Default Values works for identity columns on the standard version of SQL. I can't see any reason why it wouldn't work on CE.
In your case you would do something like this:
Insert Into Target
Default Values
Edit:
This issue looks like it is specific to SQL CE.
The only other thing I could suggest would be to add another column to your table, such as DateInserted.
Insert Into Target (DateInserted)
Values (getdate())
This should then insert a new row thus generating a new ID.
If you can change your table structure then you could us a UniqueIdentifier instead.
Create Table Target
(
IDColumn uniqueidentifier not null
)
Insert Into Target
Values (newId())

ID numbers generation

i am trying to code for my system in NetBeans IDE 6.5 to auto generate ID numbers for me like autonumbers in Ms Access. does any one have any ideas about going about that?i mean code for it.
What database system are you using? If it's something SQL based:
CREATE TABLE $tblname (id int(10) NOT NULL auto_increment PRIMARY KEY (id))
Try using auto_increment , such as in the example above.
If you're using JavaDB you need the GENERATED AS IDENTITY option on a field in your CREATE TABLE statement.
In the Windows API you can create a Guid. I'm sure there is some similar UID API for Netbeans.
If you're using Oracle, you will need a sequence for each table.
once you have the sequence you can create a trigger like this:
Create or Replace trigger incrementOnInsert
before insert on TABLE
for each row
begin
select sequence.nextval into :new.id from dual;
end;