5

How can I add annotations/attributes to my classes or methods of classes in my model, generated from my Model.edmx file? The reason I'm asking this is because every time I add an annotation to a class Model.Designer.cs, this annotation disappears when I make changes to my Model.edmx file through the designer.

More specifically, I am looking for a way to add the AllowHtml annotation to a specific property within one of my model classes, and make it stay there even after messing around with it in the model designer view.

Here's the controller code. The Content property of the Segment class is the one causing my controller to crash when being populated with HTML.

    [FacebookAuthorize(Permissions = AuthenticationController.ExtendedPermissions, LoginUrl = "/Authentication/LogOn?ReturnUrl=~/Segment/Contribute")]
    [HttpPost]
    [ValidateInput(false)]
    public ActionResult Contribute(int id, string content)
    {

        var container = new ModelContainer();

        var parent = container.SegmentSet.SingleOrDefault(s => s.Id == id);

        var segment = new Segment();
        segment.Content = content; //this crashes with HTML data.
        segment.Owner = AuthenticationController.Authentication.GetUser(container);
        segment.TimeModified = DateTime.UtcNow;
        segment.TimePosted = DateTime.UtcNow;

        container.AddToSegmentSet(segment);

        if (!parent.Children.Contains(segment))
        {
            parent.Children.Add(segment);
            segment.Parent = parent;
        }

        container.SaveChanges();

        return RedirectToAction("Index", "Home");
    }
Mathias Lykkegaard Lorenzen
  • 15,031
  • 23
  • 100
  • 187

3 Answers3

14

You can use MetadataTypeAttribute to extend your classes:

[MetadataType(typeof(MyEdmxClassExtension))]
public partial class MyEdmxClass { }

public class MyEdmxClassExension
{
    [AllowHtml] // Add the attributes you want to find on your property
    public string ThePropertyYouWantToExtend { get; set; }
}

This code goes in a separate file than the one generated or you will lose it, too, of course.

Edit

In response to the comments below, I believe you have two questions. Your original question is answered using the MetadataTypeAttribute and, for the second, you should probably open another question.

Jim D'Angelo
  • 3,952
  • 3
  • 25
  • 39
  • Except of the fact that it doesn't work. My controller still crashes when it receives HTML data. Can you help me with this? I posted the controller code in the original post. – Mathias Lykkegaard Lorenzen Dec 31 '11 at 17:34
  • What is crashing? Can you provide the exception information? Looking at your controller, I'm not sure what you're attempting to gain by using the `AllowHtmlAttribute` since you're taking in and int and a string. – Jim D'Angelo Dec 31 '11 at 17:36
  • There are no exception details. I just get a blank page with no HTML output in the browser. However, if (as shown in the original post) the content property is not set to HTML content, it works just fine. – Mathias Lykkegaard Lorenzen Dec 31 '11 at 17:37
  • 1
    How are you displaying the content? In reality, my solution does answer your original question--if you are having a different issue, that is a different question. – Jim D'Angelo Dec 31 '11 at 17:39
0

Another possible solution is to use a Metadata and Data Annotations Template as described in here

Community
  • 1
  • 1
usefulBee
  • 9,250
  • 10
  • 51
  • 89
0

Your annotations inside the auto-generated file will always disappear when the model is changed in the designer.

You can extend your class with custom properties by writing a partial class and storing it in a separate file.

public partial class MyClass
{
    [AllowHtml]
    public int MyAnnotatedProperty
    {
        get { ... }
        set { ... }
    }
}

If you want to annotate an auto-generated property have a look at James D'Angelo's answer above.

Community
  • 1
  • 1
Dennis Traub
  • 50,557
  • 7
  • 93
  • 108