Entity Framework & SQL Compact Edition - how do I get ntext? - entity-framework

The answer to my question should be quite obvious, but I cannot find it. I have a edmx file that has one table. There is a field of type string. EF always generates nvarchar for that (which is kind of expected), but I need an ntext instead of nvarchar for that field as 4000 is too small for me.
So tell me - what is the proper way to tell EF to generate ntext fields?
PS Using Entity Framework 4, SQL CE 3.5

I guess you are using model first, don't you? You can simply create custom T4 template for SQL DDL generation and include logic which will use NTEXT when field is defined with max size.
Default template is on:
C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\DBGen\SSDLToSQL10.tt
Just copy this template and find the logic where data type is created. Once you have your template change DDL Generation Template in model properties (in the designer) to your modified version.
There is much more you can do with generation template because you can add some annotations to your model (XML) and use them for custom logic in the SQL generation process.

Just set the property "MaxLength" in the Designer to "Max". This will generate a ntext field in the SQL CE DB.

If your project contains an ADO.Net Entity Data Model (.edmx) then see Ladislav's excellent answer.
But if you're using the Code First libraries and your project doesn't contain a .edmx then you can use the System.ComponentModel.DataAnnotations.ColumnAttribute to specify the column type:
using System.ComponentModel.DataAnnotations;
public class Note {
[Column("Note", TypeName="ntext")]
public string Note { get; set; }
}

Related

Npgsql data type mapping of Character from Postgresql to .NET Core 2.0

In a PostgreSQL database I have a RFC column which is sort of a code to identify Enterprises and people in Mexico (for taxes purposes), code that I need to store in my database. The format of this 'code' it's like the next one:
AAAXXXXXXAXX -> where A's are letters and X's are numbers.
I want to store RFC column as primary key. As far as I've searched, Postgres Character data type is good for this, and I have the SQL query of it as pgAdmin4 generates:
rfc character(13) COLLATE pg_catalog."default" NOT NULL,
CONSTRAINT pk_empresas PRIMARY KEY (rfc)
But, inside Visual Studio using Package Manager Console and the next command:
Scaffold-DbContext "Host=localhost;Database=database;Username=pgadmin;Password=xxxx" Npgsql.EntityFrameworkCore.PostgreSQL -force
It generates my models that are mapped as the tables in my database.
The question here is, how can I work correctly if .NET char datatype only holds a single character and the property RFC is generated as follows?
public char Rfc { get; set; }
This first approach stores only the first character. I can see it in pgAdmin4
Database record saved
I've tried to change Rfc property data type(as I know that some .NET data types can match to others in PostgreSQL as we can see in the next link Npgsql Supported Types ) as string like this:
public string Rfc { get; set; }
But this Table is also related to another 4 or 5 Tables in my Database, and I get too many errors when I try to change the data type of this property in my model (as it is also related in models).
I have to say that I have tried this but it throws an exception.
Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries.
Edit 1:
I'm using Npgsql.EntityFrameworkCore.PostgreSQL version 2.0.1
This has been fixed for version 2.0.2 of the Npgsql EF Core provider which will be released very soon.
See https://github.com/npgsql/Npgsql.EntityFrameworkCore.PostgreSQL/issues/370 for the github issue.

Entitfy Framework DbContext and xxxEntities?

I have a SQL server db ( with tables etc) and ive installed ef6 in order to use async stuff ( p.s. im new to ef).
So I added this :
played with the wizard and created a valid edmx files.
My db name is DUMP so it added Dumpentities suffix :'
so now i can do :
de = new DumpEntities1();
var data=de.AgeGroups.ToList()
But why don't I Have DbContext ? like I see in many places ?
Is xxxEntityes is a replacement for DbContext ?
cause it seems i can do all actions with xxEntites ...
edit
Ive searched "dbcontext" in my solution and apprently i do have it :
So what is going on here?
does using xxxEntiyies is the new way ?( and not doing xxxContext = new xxxContext()...even if I wanted - I dont have it...)
You should not use DbContext directly (that will not make sense) in Entity Framework. Instead you use your own custom context - class inherited from DbContext which holds sets specific to your application. When you use database first approach this custom entity class will be generated based on edmx file data, which in his turn will be generated based on database schema.
Regarding to naming... its not obvious but custom context which will be generated, will have same name as connection string name when you are creating edmx file:
Actually this will be default name for Entity Container of your conceptual entity model. If you will open edmx file in designer and take a look on its properties, you will see:
If you will change this name, context will be re-generated with name you have provided.

Change tables generated by Entity Framework and Code First

I know that you can change T4 templates used to generate the classes using Entity Framework 5.0 if you are using Model First. How would you change the template used to generate the tables when you use code first?
When using Code First there are no templates to generate tables. To set table names you can use TableAttribute or configure the table for the given entity with ToTable method in your OnModelCreating override.

ODAC 11.2 Rel. 4 table and field names casing and numbers issues in EF 4.1

Just migrated ODP.NET 11.2 Release 4 from Devart DotConnect for Oracle http://www.devart.com/dotconnect/oracle/ but I got so many issues.
Number(1,0) data type must be translated as bool instead of Int16 or
short. Devart translated it correctly. I solved the issue by adding
<add name="bool" value="edmmapping number(1,0)"/>
Number(9,2) is translated as Decimal while it should be double. Devart translate it
double and this is correct. We need double datatype for this. Table
names are not adjusted if transformed as C# Class. For example I
have table "PRODUCTS" EF made a class "PRODUCTS" also with the same
casing. Devart on the other hand created "Products" which is exactly
the correct naming translation.
Field names are not adjusted if transformed as C# Class properties. For example table "PRODUCTS" has field PRODUCT_NAME it should be ProductName if translated into C#
Class property. Devart does it correctly.
Table Names and Classnames should be in Proper Case if translated to C#.
Underscores for table names should be removed.
For the bool mapping issue, you can find my solution here:
https://forums.oracle.com/forums/message.jspa?messageID=10402873#10402873
For the double mapping issue, there seem to be no solutions so far: https://forums.oracle.com/forums/message.jspa?messageID=10402783#10402783
Regarding the names transformation issue, Microsoft's provider for MSSQL doesn't do that automatically either.
You can rename everything manually, however, that is very annoying to do.
(Good to know that Devart can do that automatically.)

importing foreign key columns into model from visual studio 2008

I got a question about foreign key when "importing" the db into visual studio.
my development environment:
SQL 2005/2008/2008R2
Visual Studio 2008 ASP.NET MVC 2 with .Net framework 3.5 SP1
was following the MVC music store V1.0
in the tutorial pdf, page 50, it says when we creating the "Entity Data Model" at the last stage, we should select "Include foreign key columns in the model.
however, i am using VS2008 and when at this stage there is no such foreign key option to choose.
plz notice that inside the red circle, it should contain (AlbumId, GenreId, ArtistId, Title, Price, AlbumArtURL) as shown in tutotial
but my model is missing GenreId and ArtistId.
how to deal with it?
I encountered a problem later when the i creating edit form and need to use something like [[[[<%= Html.LabelFor(model => model.GenreId) %>]]]]] it is not working.
so i need the GenreId and ArtistId to appear in the Album. i guess it is about the foreign key problem...
do you know how to solve it?
Thanks a lot!!!
It is not possible. Importing foreign key columns is feature of EFv4 so you need .NET 4.0 and Visual Studio 2010 to use it.
In your case you must access FK value through EntityObject based API. Your entity should have property like GenreReference so you can use something like:
int genreId = (int)entity.GenreReference.EntityKey.EntityKeyValues[0];