实体框架:编写自定义数据注释来更改值的 CASE

发布于 2024-11-10 11:09:07 字数 360 浏览 4 评论 0原文

class DemoUser
{
    [TitleCase]
    public string FirstName { get; set; }

    [TitleCase]
    public string LastName { get; set; }

    [UpperCase]
    public string Salutation { get; set; }

    [LowerCase]
    public string Email { get; set; }
}

假设我有上面写的演示类,我想创建一些自定义注释,如 LowerCase、UpperCase 等,以便其值自动转换。这样做将使我能够在其他类中使用这些注释。

class DemoUser
{
    [TitleCase]
    public string FirstName { get; set; }

    [TitleCase]
    public string LastName { get; set; }

    [UpperCase]
    public string Salutation { get; set; }

    [LowerCase]
    public string Email { get; set; }
}

Suppose i have demo-class as written above, i want to create some custom annotations like LowerCase,UpperCase etc so that its value gets converted automatically. Doing this will enable me to use these annotations in other classes too.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜心 2024-11-17 11:09:07

正如拉迪斯拉夫所暗示的,这是两个问题合二为一的。

假设您遵循 Jefim 链接中创建属性的方法,并假设您调用这些创建的属性类“UpperCaseAttribute”、“LowerCaseAttribute”和“TitleCaseAttribute”,则以下 SaveChanges() 重写应该在 EF 4.3(当前版本)中工作截至本回答发布时)。

public override int SaveChanges()
{
    IEnumerable<DbEntityEntry> changedEntities = ChangeTracker.Entries().Where(e => e.State == System.Data.EntityState.Added || e.State == System.Data.EntityState.Modified);

    TextInfo textInfo = Thread.CurrentThread.CurrentCulture.TextInfo;
    changedEntities.ToList().ForEach(entry =>
    {
        var properties = from attributedProperty in entry.Entity.GetType().GetProperties()
                        where attributedProperty.PropertyType == typeof (string)
                        select new { entry, attributedProperty,
                                    attributes = attributedProperty.GetCustomAttributes(true)
                                        .Where(attribute => attribute is UpperCaseAttribute || attribute is LowerCaseAttribute || attribute is TitleCaseAttribute) 
                                };
        properties = properties.Where(p => p.attributes.Count() > 1);

        properties.ToList().ForEach(p =>
        {
            p.attributes.ToList().ForEach(att =>
            {
                if (att is UpperCaseAttribute)
                {
                    p.entry.CurrentValues[p.attributedProperty.Name] = textInfo.ToUpper(((string)p.entry.CurrentValues[p.attributedProperty.Name]));
                }
                if (att is LowerCaseAttribute)
                {
                    p.entry.CurrentValues[p.attributedProperty.Name] = textInfo.ToLower(((string)p.entry.CurrentValues[p.attributedProperty.Name]));
                }
                if (att is TitleCaseAttribute)
                {
                    p.entry.CurrentValues[p.attributedProperty.Name] = textInfo.ToTitleCase(((string)p.entry.CurrentValues[p.attributedProperty.Name]));
                }
            });
        });
    });
    return base.SaveChanges();
}

As Ladislav implied, this is two questions in one.

Assuming you follow the recipe for creating attributes in Jefim's link, and assuming you're calling those created attribute classes "UpperCaseAttribute", "LowerCaseAttribute", and "TitleCaseAttribute", the following SaveChanges() override should work in EF 4.3 (the current version as of the time of this answer post).

public override int SaveChanges()
{
    IEnumerable<DbEntityEntry> changedEntities = ChangeTracker.Entries().Where(e => e.State == System.Data.EntityState.Added || e.State == System.Data.EntityState.Modified);

    TextInfo textInfo = Thread.CurrentThread.CurrentCulture.TextInfo;
    changedEntities.ToList().ForEach(entry =>
    {
        var properties = from attributedProperty in entry.Entity.GetType().GetProperties()
                        where attributedProperty.PropertyType == typeof (string)
                        select new { entry, attributedProperty,
                                    attributes = attributedProperty.GetCustomAttributes(true)
                                        .Where(attribute => attribute is UpperCaseAttribute || attribute is LowerCaseAttribute || attribute is TitleCaseAttribute) 
                                };
        properties = properties.Where(p => p.attributes.Count() > 1);

        properties.ToList().ForEach(p =>
        {
            p.attributes.ToList().ForEach(att =>
            {
                if (att is UpperCaseAttribute)
                {
                    p.entry.CurrentValues[p.attributedProperty.Name] = textInfo.ToUpper(((string)p.entry.CurrentValues[p.attributedProperty.Name]));
                }
                if (att is LowerCaseAttribute)
                {
                    p.entry.CurrentValues[p.attributedProperty.Name] = textInfo.ToLower(((string)p.entry.CurrentValues[p.attributedProperty.Name]));
                }
                if (att is TitleCaseAttribute)
                {
                    p.entry.CurrentValues[p.attributedProperty.Name] = textInfo.ToTitleCase(((string)p.entry.CurrentValues[p.attributedProperty.Name]));
                }
            });
        });
    });
    return base.SaveChanges();
}
生活了然无味 2024-11-17 11:09:07

您可以在 EF 上下文中重写 SaveChanges 方法(如果您使用默认代码生成,只需编写一个分部类)。像下面这样:

public partial class MyEntityContext
{
    public override int SaveChanges(SaveOptions options)
    {
        IEnumerable<ObjectStateEntry> changedEntities = 
            this.ObjectStateManager.GetObjectStateEntries(
                System.Data.EntityState.Added | System.Data.EntityState.Modified);
        // here you can loop over your added/changed entities and 
        // process the custom attributes that you have
        return base.SaveChanges(options);
    }
}

You can override the SaveChanges method in your EF context (if you use default code-generation just write a partial class). Something like the following:

public partial class MyEntityContext
{
    public override int SaveChanges(SaveOptions options)
    {
        IEnumerable<ObjectStateEntry> changedEntities = 
            this.ObjectStateManager.GetObjectStateEntries(
                System.Data.EntityState.Added | System.Data.EntityState.Modified);
        // here you can loop over your added/changed entities and 
        // process the custom attributes that you have
        return base.SaveChanges(options);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文