Entity Framework & SQL Compact Edition - how do I

2019-07-30 13:51发布

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

3条回答
Evening l夕情丶
2楼-- · 2019-07-30 14:01

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

查看更多
相关推荐>>
3楼-- · 2019-07-30 14:16

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; }

}
查看更多
叛逆
4楼-- · 2019-07-30 14:18

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.

查看更多
登录 后发表回答