OData元数据公开了所有实体.Net Core

时间:2018-03-25 22:49:36

标签: c# asp.net-core .net-core odata

我试图将Odata配置为仅在我的上下文中公开某些实体。我正在使用.Net-Core。使用7.0.0 Beta2。封装

class ExamAttempt:
    def __init__(self, id, name, correct, total):
        self.id = id
        self.name = name
        self.correct = correct
        self.total = total
        self.score = (self.correct / float(self.total))

    def __repr__(self):
        return "<ExamAttempt: Id={}, Student={}, Score={}>".format(self.id, self.name, self.score)

class Exam:
    def __init__(self, name, questions):
        self.name = name
        self.attempts = []
        self.questions = questions
        self.num_questions = len(questions)

    def __str__(self):
        return "<Exam ({})>".format(self.name)

    def load(self, filename):
        pass

    def saveAttemptsToFile(self, filename):
        pass

    def record_attempt(self, student_name, num_correct):
        id = len(self.attempts) + 1
        self.attempts.append(
            ExamAttempt(id, student_name, num_correct, self.num_questions))

    def get_student_attempt(self, student_name):
        for att in self.attempts:
            if student_name == att.name:
                return att

    def get_average_score(self):
        return "homework" 

    def get_results_by_score(self):
        return sorted(self.attempts, key=lambda x: x.score, reverse=True)

    def get_attempts_by_name(self):
        return sorted(self.attempts, key=lambda x: x.name)

if __name__ == '__main__':
    questions = ['Question?' for i in range(100)] # Generate 100 "questions" = 100%
    exam = Exam('Programming 101', questions)
    data = [('Rick', 89), ('Pat', 79), ('Larry', 82)]

    for name, correct in data:
        exam.record_attempt(name, correct)

    for attempt in exam.get_results_by_score():
        print("{} scored {}".format(attempt.name, attempt.score))

当我导航到$ Metadata页面时,我可以看到我公开的实体以及我的数据库环境中的所有其他实体

services.AddOData();

//...

app.UseMvc(routeBuilder =>
{
    routeBuilder.MapODataServiceRoute("odata", null, GetModel());
    routeBuilder.EnableDependencyInjection();
});

public static IEdmModel GetModel()
{
    var builder = new ODataConventionModelBuilder();
    var skillSet = builder.EntitySet<Skill>(nameof(Skill));
    skillSet.EntityType.Count().Filter().OrderBy().Expand().Select();
    builder.ContainerName = "DefaultContainer";

    return builder.GetEdmModel();
}

我如何只公开在启动时注册的实体?

1 个答案:

答案 0 :(得分:0)

事实证明这是因为我的导航属性。如果您公开导航属性,它将通过所有已连接的导航属性

public static IEdmModel GetModel()
{
    var builder = new ODataConventionModelBuilder();
    var skillSet = builder.EntitySet<Skill>(nameof(Skill));
    skillSet.EntityType.Ignore(x => x.RequestNegotiations);
    skillSet.EntityType.Ignore(x => x.UserSkills);

    builder.ContainerName = "DefaultContainer";

    return builder.GetEdmModel();
}