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

ε祈祈猫儿з 提交于 2019-12-12 23:05:17

问题


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


回答1:


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.




回答2:


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




回答3:


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

}


来源:https://stackoverflow.com/questions/6150525/entity-framework-sql-compact-edition-how-do-i-get-ntext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!