我有一个FileForUploading应该上传到数据库的类。
public class FileForUploading
{
public FileForUploading(string filename, Stream stream)
{
this.Filename = filename;
this.Stream = stream;
}
public string Filename { get; private set; }
public Stream Stream { get; private set; }
}
Run Code Online (Sandbox Code Playgroud)
我正在使用实体框架将其转换FileForUploadingEntity
为一个非常简单的类,但只包含该Filename属性。我不想将其存储Stream在内存中,而是将其直接上传到数据库。
将数据Stream直接“流式传输”到数据库的最佳方式是什么?
到目前为止,我想出了这个
private void UploadStream(string name, Stream stream)
{
var sqlQuery = @"UPDATE dbo.FilesForUpload SET Content =@content WHERE Name=@name;";
var nameParameter = new SqlParameter()
{
ParameterName = "@name",
Value = name
};
var contentParameter …Run Code Online (Sandbox Code Playgroud) 我有两个实体如下:
public class FirstEntity
{
public int Id { get; set; }
public ICollection<SecondEntity> SecondEntityCollection { get; set; }
}
public class SecondEntity
{
[Key]
[Column(Order = 0)]
public int FirstEntitySomeId { get; set; }
[Key]
[Column(Order = 1)]
public int SecondEntityId { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我想SecondEntity使用 EF6生成要添加到数据库的迁移。问题是 EF 生成迁移如下:
CreateTable("dbo.SecondEntity",
c => new
{
SecondEntityId = c.Int(nullable: false),
FirstEntitySomeId = c.Int(nullable: false),
FirstEntity_Id = c.Int(),
})
.PrimaryKey(t => new {t.SecondEntityId, t.FirstEntitySomeId})
.ForeignKey("dbo.FirstEntity", t => t.FirstEntity_Id)
.Index(t …Run Code Online (Sandbox Code Playgroud)