小编Bro*_*Cow的帖子

如何在 EF Code First 中声明数据类型为 Medium Blob 的表上的字段

我使用 dotnet Core EntityFramework 使用 SapientGuardian.EntityFrameworkCore.MySql

我有一个数据库实体,它有一个名为 ProfileImage 的属性存储为byte[]... 下面的摘录

public class ProfileEntity
{

    /// Gets or sets the full name.
    /// </summary>
    public string FullName { get; set; }

    /// <summary>
    /// A Byte Array with the profile image Bitmap
    /// </summary>
    public byte[] ProfileImage { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

当它在 MySql 数据库中创建时,它会创建一个 BLOB 数据类型。

我的问题是如何将其设置为 MediumBlob?

编辑: 迁移(我忘了运行)产生了以下内容:

public partial class AddMediumBlob : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.AlterColumn<byte[]>(
            name: "ProfileImage",
            table: "ProfileEntity", …
Run Code Online (Sandbox Code Playgroud)

c# mysql entity-framework entity-framework-core

5
推荐指数
1
解决办法
1956
查看次数

在 EF Core 迁移中,设置级联删除时,它不会在数据库中强制执行吗?

为什么在父实体和子实体之间设置级联删除时,不会在迁移中创建级联?

常见博客/帖子示例:

class Blog
{
    public int Id { get;set; }
    public IList<Post> Posts { get;set;}
}

class Post
{
    public int Id { get;set; }
    public Blog Blog { get;set;}
}
Run Code Online (Sandbox Code Playgroud)

在 EntityTypeConfiguration 文件中

public override void Configure(EntityTypeBuilder<Notification> builder)
    {
 
        builder.HasMany(n => n.Posts).WithOne(e => e.Blog)
            .OnDelete(DeleteBehavior.Cascade);
    }
Run Code Online (Sandbox Code Playgroud)

为什么要创建迁移脚本?

            ...
            migrationBuilder.AddForeignKey(
            name: "FK_Posts_Blogs_BlogId",
            table: "Posts",
            column: "BlogId",
            principalTable: "Blogs",
            principalColumn: "Id",
            onDelete: ReferentialAction.Restrict);
            ...
Run Code Online (Sandbox Code Playgroud)

请注意

onDelete:ReferentialAction.Restrict

我知道 EF 实际上会在内部执行级联删除,只要您包含子对象以供其工作,但为什么不利用数据库服务级联删除能够通过一个命令而不是 1 + n SQL 命令来删除它,即1 x 博客记录和 nx 帖子。

想象一下,有数千个帖子,而您正在删除一个博客。

c# entity-framework-core .net-core

5
推荐指数
1
解决办法
1273
查看次数

在计算符号和两个数字的答案时,我怎么能使用linq

最近我接受了一次采访,要求写一个方法来计算前缀格式化字符串的答案.

采访的反馈是我应该使用Linq

我有点困惑,我应该使用linq,并想知道是否有人可以帮助我.

问题

签名方法应该是:

int Calculate(string expression)
Run Code Online (Sandbox Code Playgroud)

我不需要验证输入,但给出了一些例子

  • "+ 3 4"应该给7
  • " - 2 10"应该给-8
  • "*10 -20"应该给-200
  • "/ 6 2"应该给3

我的解决方案 - 来自记忆

所以我创建了单元测试并在TDD庄园中开发了代码

[TestFixture]
public class UnitTest1
{
    [Test]
    public void TestMethod_Add()
    {
        var calculator = new Calculator();
        var result = calculator.Calculate("+ 4 2");
        var expected = 6;

        Assert.AreEqual(expected, result);
    }

    [Test]
    public void TestMethod_Sub()
    {
        var calculator = new Calculator();
        var result = calculator.Calculate("- 2 10");
        var expected = -8;

        Assert.AreEqual(expected, result);
    }

    [Test]
    public void TestMethod1_mult() …
Run Code Online (Sandbox Code Playgroud)

c# linq

4
推荐指数
2
解决办法
144
查看次数

使用 .net.core 创建动态表单

我需要为不同的客户端提供不同的表单,这些表单都可以在后台配置(最后在数据库中)

我最初的想法是为“Form”创建一个对象,它有一个“FormItem 字典”来描述表单字段。

然后我可以通过执行以下操作来新建一个动态表单(这将来自数据库/服务):

   private Form GetFormData()
    {
        var dict = new Dictionary<string, FormItem>();
        dict.Add("FirstName", new FormItem()
        {
            FieldType = Core.Web.FieldType.TextBox,
            FieldName = "FirstName",
            Label = "FieldFirstNameLabel",
            Value = "FName"
        });
        dict.Add("LastName", new FormItem()
        {
            FieldType = Core.Web.FieldType.TextBox,
            FieldName = "LastName",
            Label = "FieldLastNameLabel",
            Value = "LName"
        });
        dict.Add("Submit", new FormItem()
        {
            FieldType = Core.Web.FieldType.Submit,
            FieldName = "Submit",
            Label = null,
            Value = "Submit"
        });

        var form = new Form()
        {
            Method = "Post",
            Action = "Index",
            FormItems = dict …
Run Code Online (Sandbox Code Playgroud)

forms model-view-controller asp.net-mvc custom-model-binder

4
推荐指数
1
解决办法
1万
查看次数

如何将数据传递到 ng-Container

对不起。我是 Angular 的新手:)

我正在尝试将一些数据传递到模板中

这是调用sharedComponent的组件

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'sym-test',
  templateUrl: './test.component.html',
  styles: []
})
export class TestComponent implements OnInit {

    items = [
        '111',
        '222',
        '333'
    ];

  constructor() { }

    ngOnInit() {
  }
}
Run Code Online (Sandbox Code Playgroud)

<p>test works!</p>

<sym-shared [items]="items">
    <ng-template LiTemplateDirective>
        <b>{{item}}</b>
    </ng-template>
</sym-shared>
Run Code Online (Sandbox Code Playgroud)

import { Component, OnInit, Input, ContentChild, TemplateRef } from '@angular/core';
import { LiTemplateDirective } from '../li-template.directive';

@Component({
    selector: 'sym-shared',
    templateUrl: './shared.component.html',
})
export class SharedComponent implements OnInit {
    @Input() items: any[]; …
Run Code Online (Sandbox Code Playgroud)

angular-template angular

2
推荐指数
1
解决办法
4438
查看次数