有没有人知道如何使用System.ComponentModel DefaultValue属性为DateTime属性指定默认值?
例如我试试这个:
[DefaultValue(typeof(DateTime),DateTime.Now.ToString("yyyy-MM-dd"))]
public DateTime DateCreated { get; set; }
Run Code Online (Sandbox Code Playgroud)
并且它期望值是一个恒定的表达式.
这是在使用ASP.NET动态数据的上下文中.我不想构建DateCreated列,但只是提供DateTime.Now,如果它不存在.我使用实体框架作为我的数据层
干杯,
安德鲁
我有一个使用EF Core 2.1.0的ASP.NET Core 2.1.0应用程序.
如何使用Admin用户为数据库播种并为其授予管理员角色?我找不到任何关于此的文件.
我正在使用 ASP.NET Core 2.2 并且我正在使用模型绑定来上传文件。
这是我的UserViewModel
public class UserViewModel
{
[Required(ErrorMessage = "Please select a file.")]
[DataType(DataType.Upload)]
public IFormFile Photo { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是我的视图
@model UserViewModel
<form method="post"
asp-action="UploadPhoto"
asp-controller="TestFileUpload"
enctype="multipart/form-data">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input asp-for="Photo" />
<span asp-validation-for="Photo" class="text-danger"></span>
<input type="submit" value="Upload"/>
</form>
Run Code Online (Sandbox Code Playgroud)
最后这是MyController
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> UploadPhoto(UserViewModel userViewModel)
{
if (ModelState.IsValid)
{
var formFile = userViewModel.Photo;
if (formFile == null || formFile.Length == 0)
{
ModelState.AddModelError("", "Uploaded file is empty or …
Run Code Online (Sandbox Code Playgroud) 如果我跑 dotnet ef add testmigration
我收到这个警告: The EF Core tools version '2.1.0-rtm-30799' is older than that of the runtime '2.1.1-rtm-30846'. Update the tools for the latest features and bug fixes.
所以我检查了我的csproj文件:
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.1" />
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)
这对我来说是正确的,版本2.1.1.所以我在这里检查了文档
他们建议csproj中的工具条目需要有这个包:
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.1.1" />
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)
现在dotnet restore
抱怨说:
warning : The tool 'Microsoft.EntityFrameworkCore.Tools.DotNet' is now included in the .NET Core SDK. Information on resolving this …
我在 Xamarin.iOS 应用程序中使用 Entity Framework Core。
在包含在 iOS 应用程序和其他应用程序之间共享的代码 (.netstandard 2.0) 的核心项目中,我想知道是否需要迁移,以便我也可以执行一些其他操作。
这是上下文:
public void Initialize()
{
using (var dbContext = new MyDbContext(m_dbContextOptions))
{
--> bool isNeeded = demoTapeDbContext.Database.IsMigrationNeeded()
demoTapeDbContext.Database.Migrate();
}
}
Run Code Online (Sandbox Code Playgroud)
我发现的最接近的是调用该方法GetPendingMigrationsAsync()
并检查待处理迁移的数量,但我不确定这是否是在实体框架中进行此类检查的最安全方法:
public async Task InitializeAsync()
{
using (var dbContext = new MyDbContext(m_dbContextOptions))
{
bool isMigrationNeeded = (await demoTapeDbContext.Database.GetPendingMigrationsAsync()).Any();
demoTapeDbContext.Database.Migrate();
}
}
Run Code Online (Sandbox Code Playgroud) 看起来很简单,但我不知道如何告诉 EF 核心不要创建具有自动递增标识列的实体的主键。我想自己插入我自己的主键值。我意识到我可以使用属性来做到这一点,但我想知道如何通过 fluent API 设置行为。我从 Property() 方法看到 UseSqlServerIdentityColumn() 方法,但我需要将其关闭(而不是打开)。我也试过下面的代码,但它不起作用。
context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT dbo.Employees ON");
context.SaveChanges();
context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT dbo.Employees OFF");
Run Code Online (Sandbox Code Playgroud) 将asp.net核心api添加到我正在处理的现有项目时,出现以下错误。
“直接检测到Microsoft.EntityFrameworkCore.Install /引用Microsoft.EntityFrameworkCore 2.2.1的版本冲突,以解决该问题”
我尝试添加Nuget程序包,但在许多不同的程序包中出现了进一步的版本冲突,并且该过程始终失败。起初我以为这可能是我的项目的问题,所以我从头开始了一个新的解决方案,并设法通过几个简单的步骤来复制该问题。
我已经阅读了以下文章,但是它的解决方案对我不起作用,因为我的任何csproj文件都没有引用“ Microsoft.EntityFrameworkCore”。 检测到NuGet软件包的版本冲突。
我还重新安装了.net SDK,重新启动了100万次,但仍然无法理解问题所在。
我正在使用 EF Core 值转换。
https://docs.microsoft.com/en-us/ef/core/modeling/value-conversions
我写了一个 JSON 序列化器转换器,如下所示:
private static ValueConverter<T, String> JsonValueConverter<T>()
{
ValueConverter<T, String> jsonConverter = new ValueConverter<T, String>(
v => JsonConvert.SerializeObject(v),
v => JsonConvert.DeserializeObject<T>(v));
return jsonConverter;
}
Run Code Online (Sandbox Code Playgroud)
在应用程序中实现:
protected override void OnModelCreating(ModelBuilder mb)
{
...
mb.Entity<MyObject>()
.Property(p => p.MySerializableObject)
.HasConversion(JsonValueConverter<MySerializableObject>());
...
}
Run Code Online (Sandbox Code Playgroud)
有用。
但是,当您对序列化对象内的属性进行更改时,EF Core 更改跟踪不会获取对MySerializableObject
.
我假设有某种方法可以在对象级别强制执行此操作。我试图实施IEqualityComparer
,MySerializableObject
但更改跟踪没有开始工作。
我正在使用 Asp.Net Core 2.1、Mvc、c#、EF Core with Code First 和 Migrations。
我正在尝试构建一个在Migration.Up()
方法中具有复合主键的表:
migrationBuilder.CreateTable(
name: "TagValueAttributes",
columns: table => new {
TagValueID = table.Column<Int64>(nullable: false),
Identifier = table.Column<string>(nullable: false, unicode: true, maxLength: 256),
Value = table.Column<string>(nullable: true, unicode: true, maxLength: 2048)
},
constraints: table => {
table.PrimaryKey(
name: "PK_TagValueAttributes",
columns: // what goes here???
)
}
);
Run Code Online (Sandbox Code Playgroud)
我不知道为调用的columns
参数指定什么constraints
table.PrimaryKey()
。我想要 columns TagValueID
,并Identifier
形成复合键。
我需要为columns
参数指定什么?
composite-primary-key asp.net-core asp.net-core-2.1 ef-core-2.1 entity-framework-migrations
我正在使用EF Core 2.1
这是我最初的模型定义。
public class Customer //Parent
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public BankAccount BankAccount { get; set; }
}
public class BankAccount
{
public int Id { get; set; }
public string Branch { get; set; }
public string AcntNumber { get; set; }
public DateTime CreatedDate { get; set; }
public int CustomerId { get; set; }
public Customer …
Run Code Online (Sandbox Code Playgroud) c# asp.net entity-framework entity-framework-core ef-core-2.1
ef-core-2.1 ×10
c# ×6
asp.net ×3
asp.net-core ×3
.net-core ×2
entity-framework-migrations ×1
json ×1