更新时间:2023-11-24 14:06:52
像运算符可以使用Contains函数执行:
var query = from p in context.Persons
其中p.Name.Contains(abc)
select p ;
索引必须由SQL添加 - EF中没有特殊的结构来创建索引。您可以从DB初始化执行此SQL。
首先,您必须实现自定义初始化程序:
code> public class MyInitializer:CreateDatabaseIfNotExists< MyContext>
{
protected override void Seed(MyContext context)
{
context.Database.SqlCommand(CREATE INDEX IX_Person_Name ON Person(Name));
}
}
然后你必须注册新的初始化器:
DbDatabase.SetInitializer< MyContext>(new MyInitializer());
I have a table Person
: id, name
I often have queries like:
select * from Person where name Like "%abc%".
I have 2 questions:
Like operator can be performed with Contains function:
var query = from p in context.Persons
where p.Name.Contains("abc")
select p;
Index must be added by SQL - there is no special construction in EF to create index. You can execute this SQL from DB initialization.
First you must implement custom initializer:
public class MyInitializer : CreateDatabaseIfNotExists<MyContext>
{
protected override void Seed(MyContext context)
{
context.Database.SqlCommand("CREATE INDEX IX_Person_Name ON Person (Name)");
}
}
Then you must register new initializer:
DbDatabase.SetInitializer<MyContext>(new MyInitializer());