我有一个主键"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?
我有一个AuthenticationStrategy类,我将在控制器构造函数中注入.
我有两个IAuthenticationProviders:InternalAuthenticationProvider和ExternalAuthenticationProvider.
在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)
有任何想法吗?
我有一个实体:
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".
如何在自引用实体上配置级联删除?