如何使用Entity-Framework code-first将实体持久保存到数据库时如何执行代码?(How can I execute code when an entity is being persisted to the database with Entity-Framework code-first?)
当使用EF / code-first将实体保存到数据库时,是否有一种很好的方法来执行代码?
我在许多实体上都有一个
Url
属性。 如果一个URL没有明确给出,我想计算一个,因为该对象是持久的,例如:public void OnModelSaving() { // If a URL has not been specified, generate one from the name. if (this.Url == null) { this.Url = Helper.GenerateSafeUrl(this.Title); } }
理想情况下,我希望所有代码都存储在模型中,但由于我没有EF拥有的基类/部分类,我怀疑如果可能的话,我必须在其他地方注册/连接它。 问题是 - 是否有可能,如果是这样,我该如何连接它?
Is there a nice way to execute code when an entity is being saved to the database using EF/code-first?
I have a
Url
property on many of my entities. If a URL has not been given explicitly, I would like to calculate one as the object is persisted, eg.:public void OnModelSaving() { // If a URL has not been specified, generate one from the name. if (this.Url == null) { this.Url = Helper.GenerateSafeUrl(this.Title); } }
Ideally, I'd like all this code to be stored inside the Model, but since I don't have an EF-owned base/partial class, I suspect if it's possible, I'd have to register/wire it up elsewhere. Question is - is it possible, and if so, how do I wire it up?
原文:https://stackoverflow.com/questions/6008407
最满意答案
您可以使用
ZipInputStream
和ZipOutputStream
(在java.util.zip
包java.util.zip
)来读取和写入ZIP文件。如果数据位于字节数组中,则可以从
ByteArrayInputStream
读取这些数据,或者写入指向输入和输出字节数组的ByteArrayOutputStream
。You can use
ZipInputStream
andZipOutputStream
(in the packagejava.util.zip
) to read and write from ZIP files.If you have the data in a byte array, you can let these read from a
ByteArrayInputStream
or write to aByteArrayOutputStream
pointing to your input and output byte arrays.
相关问答
更多-
从byte []创建zip文件(Create zip file from byte[])[2022-03-30]
稍微玩了一会儿,阅读后,我才明白了这一点。 下面是如何创建一个带有多个文件的压缩文件(压缩文件),而无需将任何临时数据写入磁盘: using (var compressedFileStream = new MemoryStream()) //Create an archive and store the stream in memory. using (var zipArchive = new ZipArchive(compressedFileStream, ZipArchiveMode.Update, f ... -
压缩流在关闭之前不会刷新(并且无法正确刷新)。 您将需要关闭GZipStream 。 告诉它不要关闭基础流(构造函数参数之一)将使这更容易。 //generating data int length = 100000; byte[] data = new byte[length]; for (int i = 0; i < length; i++) { data[i] = System.Convert.To ...
-
System.IO:将zip文件转换为byte []而不保存文件(System.IO: Convert zip file to byte[] without saving file)[2021-08-26]
entryName , CreateEntryFromFile的第二个参数应该是“在zip存档中创建的条目的名称”。 它不是您的zip文件的名称。 你正在压缩导入文件,但也用.zip扩展名重命名,我假设导入文件实际上不是拉链。 改为做到这一点 archive.CreateEntryFromFile(importfile, importfile); //adds the file to the zip entryName, the second parameter of CreateEntryFromFil ... -
您可以使用ZipInputStream和ZipOutputStream (在java.util.zip包java.util.zip )来读取和写入ZIP文件。 如果数据位于字节数组中,则可以从ByteArrayInputStream读取这些数据,或者写入指向输入和输出字节数组的ByteArrayOutputStream 。 You can use ZipInputStream and ZipOutputStream (in the package java.util.zip) to read and wri ...
-
您的方法decompressGZIP()需要的对象是一个byte[] 。 所以您提出的问题的基本技术答案是: byte[] b = string.getBytes(); byte[] b = string.getBytes(Charset.forName("UTF-8")); byte[] b = string.getBytes(StandardCharsets.UTF_8); // Java 7+ only 但是,您似乎正在摔跤的问题是,这并不能很好地显示出来。 调用toString()只会给你默认的O ...
-
您可以简单地使用File.ReadAllBytes静态方法,如: return File.ReadAllBytes( shapeFileZip.Name ); 从您的文件中读取。 You could simply use the File.ReadAllBytes static method like: return File.ReadAllBytes( shapeFileZip.Name ); To read from your file.
-
将.zip文件转换为字节数组并从字节数组重新创建.zip(Convert .zip file into byte array and re-create .zip from byte array)[2023-10-10]
您需要System.IO.Compression.ZipArchive类 : using (ZipArchive zip = ZipFile.Open("test.zip", ZipArchiveMode.Create)) { var entry = zip.CreateEntry("File Name.txt"); using (StreamWriter sw = new StreamWriter(entry.Open())) { sw.Write("Some Te ... -
如何将字节数组转换为zip文件并在java中的浏览器中下载(How to convert a byte array to zip file and downloaded in the browser in java)[2024-04-03]
我在这里假设你想写一个字节数组到一个zip文件。 由于发送的数据也是一个压缩文件,要保存的文件也是压缩文件,因此不应该是个问题。 2个步骤,将其保存在磁盘上并返回文件。 保存在磁盘部分: File file = new File(/path/to/directory/save.zip); if (file.exists() && file.isDirectory()) { try { OutputStream ... -
试试吧。 byte[] data = File.ReadAllBytes("D:\\z.7z"); File.WriteAllBytes("D:\\t.txt", data); // Requires System.IO byte[] newdata = File.ReadAllBytes("D:\\t.txt"); File.WriteAllBytes("D:\\a.7z", newdata); // Req ...
-
zip文件通常是使用流生成的,因此不需要将它们临时存储在文件中 - 也可以在内存中或直接流式传输到远程接收者(只有少量内存缓冲区以避免大量内存)。 接受的答案部分中的Ref Sample helperclass 如何在不使用Java保存到磁盘的情况下生成zip文件? zip files are typically generated with streams anyway, so there's no need to temporarily store them in a file - might as w ...