提问人:JD9999 提问时间:11/17/2023 最后编辑:marc_sJD9999 更新时间:11/17/2023 访问量:31
为什么我不能在实体框架中为此可选属性分配值?[关闭]
Why can I not assign this optional property a value in Entity Framework? [closed]
问:
我正在尝试遵循 Microsoft 的 EF 教程,但更改数据模型以确保我理解这些概念。但是,编译器似乎无法识别我的 nullable 属性。
我有一个具有四个属性的模型(下面附有代码),包括 ,模型中的最后一个属性可为 null。然后,在函数(数据库种子设定)中,我尝试创建模型的两个副本,一个设置了最后一个属性,另一个忽略了该属性,并将它们都添加到具有函数的数据库中。Id
Initialisation
AddRange
但是,当我运行(编译东西)时,出现以下错误:dotnet build
(文件路径)/EntitySQL/Data/DBInitialiser.cs(22,45):错误 CS1513:} 预期 [(文件路径)/EntitySQL/EntitySQL.csproj]
一旦我注释掉变量的初始化,这个和其他 16 个语法错误都会消失!
如何为这个可为 null 的对象赋值?
代码如承诺:
实体SQL/模型/文章.cs
namespace EntitySQL.Models;
public class Article
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<string> Paragraphs { get; set; }
public ICollection<Video>? Videos { get; set; }
}
实体SQL/模型/视频.cs
public class Video
{
public int Id { get; set; }
public string Name { get; set; }
public string Link { get; set; }
}
实体SQL/数据/DBInitialiser.cs
using EntitySQL.Models;
namespace EntitySQL.Data
{
public static class DBInitialiser
{
public static int Initialise(NewsContext context)
{
if (context.Articles.Any() || context.Videos.Any())
{
return 0;
}
Video video1 = new Video { Name = "Cat video", Link = "youtube cat videos.commm" }; //This link is fake
Video video2 = new Video { Name = "Big Cat video", Link = "meowtube.com" }; //This link is fake
Article bigCat = new Article
{
Name = "World's Biggest Cat Spotted Outside McDonalds",
Paragraphs = new List<String>
{
"Residents said that the apocalypse was coming.",
"At about 3am this morning, locals were woken up to what sounded like an earthquake, as a cat-like animal was seen playing with the street lights as a toy.",
"The animal was about 6 metres tall and 30 metres long, filling up the entire carpark at McDonalds CityVille. Workers were terrified and police were called in to help though, understandably, they were clueless as to how to contain the furry beast."
},
Videos = new List<Video>{ video1, video2 }; //This line is the problem!
};
Article turtleHatch = new Article {
Name = "A Turle Hatched",
Paragraphs = new List<String>{
"Yesterday, at Snow White Beach, residents were treated to one of the most adorable sights some have ever seen",
"Two baby turtles were flapping their flippers on top of each other as they pushed with all of their might to reach the ocean",
"Despite the unusual time, the birds were successfully fended of by beachgoers who cheered as the turtles made it all the way to the ocean."
}
};
context.Articles.AddRange(new List<Article> { bigCat, turtleHatch });
context.SaveChanges();
return 0;
}
public static int CreateDbIfNotExists(this IHost host)
{
using (IServiceScope scope = host.Services.CreateScope())
{
IServiceProvider provider = scope.ServiceProvider;
NewsContext context = provider.GetRequiredService<NewsContext>();
context.Database.EnsureCreated();
Initialise(context);
}
return 0;
}
}
}
答:
1赞
RezaNoei
11/17/2023
#1
从这一行中删除:;
Videos = new List<Video>{video1, video2}; //This line is the problem!
U 不能在对象初始化块中使用。您可以简单地用于分隔属性启动。;
,
VisualStudio 在发现此错误时很弱。因此,它会产生许多令人困惑的错误。
评论