小编Alg*_*das的帖子

实体框架6 GUID作为主键:不能将值NULL插入列'Id',表'FileStore'; 列不允许空值

我有一个主键"Id"的实体,它是Guid:

public class FileStore
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public string Path { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

还有一些配置:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<FileStore>().Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
    base.OnModelCreating(modelBuilder);
}
Run Code Online (Sandbox Code Playgroud)

当我尝试插入记录时,我收到以下错误:

无法将值NULL插入列'Id',表'FileStore'; 列不允许空值.INSERT失败.\ r \n语句已终止.

我不想手动生成Guid.我只想插入一条记录Id并由SQL Server生成.如果我设置.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity),则Id列不是SQL Server中的标识列.

如何配置实体框架以在SQL Server中自动生成Guid?

c# entity-framework sql-server-2008 entity-framework-6

71
推荐指数
6
解决办法
10万
查看次数

C#.Net核心依赖注入,为构造函数注入多个参数

我有一个AuthenticationStrategy类,我将在控制器构造函数中注入.

我有两个IAuthenticationProviders:InternalAuthenticationProviderExternalAuthenticationProvider.
AuthenticationStrategy构造函数中,我想注入所有提供者.
示例代码:

public class AuthenticationStrategy
{
    private readonly Dictionary<string, IAuthenticationProvider> _authenticationProviders;

    public AuthenticationStrategy(IAuthenticationProvider[] authenticationProviders)
    {
        if (authenticationProviders == null)
        {
            throw new ArgumentNullException("AuthenticationProviders");
        }

        _authenticationProviders = authenticationProviders
            .ToDictionary(x => nameof(x), x => x);
    }
}
Run Code Online (Sandbox Code Playgroud)

如何使用依赖注入注入多个提供程序?示例代码:

services.AddScoped<IAuthenticationProvider, InternalAuthenticationProvider>();
services.AddScoped<IAuthenticationProvider, ExternalAuthenticationProvider>();
services.AddScoped<AuthenticationStrategy>();
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

.net c# constructor dependency-injection .net-core

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

实体框架6自引用实体上的代码优先级联删除

我有一个实体:

public class Section : SortableEntity
{
    private ICollection<Section> _sections;

    public ICollection<Section> Sections
    {
        get
        {
            return _sections ?? (_sections = new HashSet<Section>());
        }

        set
        {
            _sections = value;
        }
    }

    public string Title { get; set; }

    public string Description { get; set; }

    public Section ParentSection { get; set; }

    public int? ParentSectionId { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

在模型创建上我有一个配置:

modelBuilder.Entity<Section>().HasOptional(x => x.ParentSection).WithMany(p => p.Sections).HasForeignKey(d => d.ParentSectionId);
Run Code Online (Sandbox Code Playgroud)

我正在尝试进行级联删除,并且我收到以下错误:"DELETE语句与SAME TABLE REFERENCE约束冲突"FK_dbo.Section_dbo.Section_ParentSectionId".

如何在自引用实体上配置级联删除?

c# entity-framework ef-code-first

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