4

I want to change the namespace of all entities exposed by my ODATA service.

Right now they've got: Core.DomainModel.Entities.Database which is a bit long when consuming it.

I've tried to set it via builder.Namespace = "MyModel"; but that had no effect.

var builder = new ODataConventionModelBuilder();
builder.Namespace = "MyModel";

I then figured I had to set it on each entity like so

builder.EntitySet<Foo>("Foo").EntityType.Namespace = "MyModel";

But that quickly gets out of hand, when I have to go through EVERY entity :(

Then I tried this

foreach (var entity in builder.EntitySets)
{
    entity.EntityType.Namespace = "MyModel";
}

But that doesn't include everything either. I noticed that ENUMs, ComplexTypes and some entities didn't get the right namespace... :/

So my question is. How do I set the namespace for every entity?

Snæbjørn
  • 8,301
  • 8
  • 49
  • 94

2 Answers2

2

One solution is to derive a new class from ODataConventionModelBuilder then implement some of the overrides.

public class ODataExtendedConventionModelBuilder : ODataConventionModelBuilder

    public override ComplexTypeConfiguration AddComplexType(Type type)
    {
        var x = base.AddComplexType(type);
        x.Namespace = Namespace;
        return x;
    }

    public override EntityTypeConfiguration AddEntityType(Type type)
    {
        var x = base.AddEntityType(type);
        x.Namespace = Namespace;
        return x;
    }

    public override EnumTypeConfiguration AddEnumType(Type type)
    {
        var x = base.AddEnumType(type);
        x.Namespace = Namespace;
        return x;
    }

    public override void AddProcedure(ProcedureConfiguration procedure)
    {
        procedure.Namespace = Namespace;
        base.AddProcedure(procedure);
    }
}

In this example I'm using the ODataModelBuilder.Namespace property to determine what namespace to configure for each entity. However you could do all sorts of things to determine namespaces to use.

Jeffrey Patterson
  • 1,652
  • 10
  • 9
0

What you have done to set namespace on each entity is right, builder.Namespace is for container's namespace, not every entity.

Fan Ouyang
  • 1,996
  • 1
  • 9
  • 13