我有一个简单的项目,最初使用SQL Server作为EF Core和Code-First方法的后端(遵循本教程:https : //docs.microsoft.com/zh-cn/ef/core/get-started/aspnetcore / new-db)
现在,我想将实现切换到SQLite。我以为可以从SQL Server运行初始迁移来创建数据库,然后将其应用于SQLite。似乎无法正常工作:例如。没有应用主键的自动增量,那么我看到一些不一致的地方(sqlite抱怨'int'哪个应该是'INTEGER'),等等。
那么,这是否意味着迁移依赖于后端?如果是,那么是否不应该在嵌套文件夹中创建它们(例如./Migrations/SQLServer)?
有人能解释一下它是如何工作的吗?
注意:对不起,我是EF Code-First和迁移领域的新手...谢谢!
sql-server sqlite entity-framework-core .net-core asp.net-core
我想知道转换的一个好办法Model来ViewModel,并ViewModel以Model没有AutoMapper或类似的东西,因为我想知道背后是什么,并学习如何做我自己.当然,模型I指的是EF生成的类.
到目前为止,我做了类似的事情,但是当涉及到嵌套类时会遇到一些问题:
// to VM
public static Author ToViewModel(EntityAuthor author)
{
if (author == null)
return null;
Author result = new Author();
result.Id = author.ATH_ID;
result.FirstName = author.ATH_FirstName;
result.LastName = author.ATH_LastName;
return result;
}
public static BlogPost ToViewModel(EntityBlogPost post)
{
if (post == null)
return null;
Experiment result = new Experiment();
result.Id = post.BP_ID;
result.Title = post.BP_Title;
result.Url = post.BP_Url;
result.Description = post.BP_Description;
result.Author = ToViewModel(post.Author);
return result;
}
// …Run Code Online (Sandbox Code Playgroud) 在下面的方法中,我想返回所选卡片的索引数组:
public class Card
{
public bool Selected { get; set; }
// ... other members here ...
}
public void int[] GetSelectedCards(Cards[] cards)
{
// return cards.Where(c => c.Selected).ToArray();
// above line is not what I want, I need their indices
}
Run Code Online (Sandbox Code Playgroud)
有谁知道一个很好的一行代码LINQ为此?可能?
更新:
有趣的是,我发现了一些东西:
return cards.Where(c => c.Selected).Select(c => Array.IndexOf(cards, c));
Run Code Online (Sandbox Code Playgroud)
你怎么看?
我有以下代码根据类型计算卡的外观:
// counters initialization
var counters = new Dictionary<int, int>();
for (int i = 2; i <= 14; i++)
counters.Add(i, 0);
// loop through and count number of appearances
for (int i = 0; i < playerCards.Length; i++)
counters[playerCards[i]]++;
// Eg output:
// playerCards = { 9, 9, 3, 3, 10 }
// counters = ( (2, 0) , (3, 2) , (4, 0) , (5, 0) , (6, 0) , (7, 0) ,
// (8, 0) , (9, 2) …Run Code Online (Sandbox Code Playgroud) c# ×3
.net ×2
linq ×2
.net-core ×1
asp.net-core ×1
asp.net-mvc ×1
model ×1
sql-server ×1
sqlite ×1
viewmodel ×1