.net C#类转换为csv,无需内部对象引用和修改原始类

disho6za  于 2023-03-24  发布在  .NET
关注(0)|答案(1)|浏览(106)

出于备份的原因,我需要写一个工作,得到所有的记录在一个数据库旧thena某些数据,保存到csv和发送到远程存储,然后从数据库中删除记录
因此,对于每个类,我创建了一个相同的类,其中包含所有在csv上写入的信息(参见下面的示例),并且没有对象引用,一切都很好。问题是:当我修改主类时,我需要记住更新模型类。
有办法避免吗?
编辑:我的问题是:有一种方法可以指定FieldQuoted,DelimitedRecord,并指定FileHelper中忽略哪个字段?
示例

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace backend.DataModel
{
    using System;
    using System.Collections.Generic;
    
    public partial class Attachment
    {
        public string Id { get; set; }
        public int AttachmentTypeCode { get; set; }
        public string AttachmentContent;
        public System.DateTime CreationDate { get; set; }
        public string Md5 { get; set; }
        public virtual AttachmentType AttachmentType1 { get; set; }
    }
}

namespace BackupDatabase.Model
{
    [DelimitedRecord(";")]
    [IgnoreEmptyLines()]
    public class AttachmentBackupModel
    {
        [FieldQuoted('"', QuoteMode.AlwaysQuoted)]
        public string Id;
        public int AttachmentTypeCode;
        [FieldQuoted('"', QuoteMode.AlwaysQuoted)]
        public string ReferenceId;
        [FieldQuoted('"', QuoteMode.AlwaysQuoted)]
        public string AttachmentContent;
        [FieldConverter(ConverterKind.Date, "dd/MM/yyyy HH:mm:ss:fff")]
        public DateTime CreationDate;
        [FieldQuoted('"', QuoteMode.AlwaysQuoted)]
        public string Md5;
        public AttachmentBackupModel() { }

        public AttachmentBackupModel(Attachment attachment)
        {
            Id = attachment.Id;
            AttachmentType = attachment.AttachmentType;
            AttachmentContent = attachment.AttachmentContent;
            Md5 = attachment.Md5;
        }
    }
}
beq87vna

beq87vna1#

使用DelimitedClassBuilder从Entity框架模型数据构建Record定义的通用解决方案。

public void Export<T>(DbContext ctx, IEnumerable<T> data)
{
    ObjectContext octx = ((IObjectContextAdapter)ctx).ObjectContext;
    EntityType storageEntityType = octx.MetadataWorkspace.GetItems(DataSpace.SSpace).OfType<EntityType>().First(es => es.Name == typeof(T).Name);

    var cb = new DelimitedClassBuilder(typeof(T).Name, ";") { IgnoreEmptyLines = true };
    foreach (var prop in storageEntityType.Properties)
    {
        if (!prop.IsComplexType ...)
        {
            var fieldBuilder = cb.AddField(prop.Name, prop.TypeName);
            if (fieldBuilder.FieldType == "System.String")
            {
                cb.LastField.TrimMode = TrimMode.Both;
                cb.LastField.QuoteMode = QuoteMode.AlwaysQuoted;
            }
            ...
        }
    }

    var engine = new FileHelperEngine(cb.CreateRecordClass());

    ...
}

相关问题