简体   繁体   中英

Override ToString in EF Metadata Class File

I'm playing around with EF4.1 in a Visual C# 2010 Express project and I can't figure out how to override ToString() in a metadata class file.

Is this even possible?

I was hoping to avoid overriding being done in the actual context class file due to them being replaced if you rebuild the context from the database.

Metadata class file content...

[MetadataType(typeof(customerSurveyMetadata))]
    public partial class customerSurvey {
        private sealed class customerSurveyMetadata {
            public string Name { get; set; }
            public override string ToString() {
                return Name;
            }
        }
    }

Context class content...

public partial class customerSurvey
    {
        ....Other Properties....
        public string Name { get; set; }
        ....Other Properties....
    }

This is making me feel like I'm making a really DUMB mistake so if someone can point me in the right direction that would be awesome. If I have to do it in the context class...so be it I'd just really like to avoid it (I've tested the override here and it works fine)...

In simplest terms my question is how do you override ToString() without putting the override inside of the context class file.

Your ToString is in the sealed class, wouldn't you want it in the outer class? Something like...

[MetadataType(typeof(customerSurveyMetadata))]
public partial class customerSurvey {
    private sealed class customerSurveyMetadata {
        public string Name { get; set; }
    }

    public override string ToString() {
        return Name;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM