实体框架的动态数据...使用 [ScaffoldColumn(true)] 通过元数据显示公共属性

发布于 2024-07-29 05:42:56 字数 2448 浏览 2 评论 0原文

...它根本不起作用。 我已经尝试了好几天了,用了所有不同的组合,但它不会让步。

当然,有些人似乎在博客中轻松地完成了这类事情,却没有看到一个问题,他们说“我们都知道你可以通过...显示扩展 EF 类的公共属性”和“如果你想扩展你的数据模型来显示计算字段,简单地......” - 对我来说没那么简单 - arghghhhhhghhhhh!!!

因此,根据所有典型示例,我的 EF 部分类如下所示:

[DisplayColumn("Name")]
[MetadataType(typeof(SaleProduct_Metadata))]
public partial class SaleProduct
{        
    public string Test
    {
        get
        {
            return "blah";
        }
    }

    public class SaleProduct_Metadata
    {
        [ScaffoldColumn(true)] 
        public string Test;
    }
}  

我的 global.asax 如下所示:

        MetaModel model = new MetaModel();
        model.RegisterContext(typeof(Sale.Models.SaleEntities), new ContextConfiguration() { ScaffoldAllTables = true });
        routes.Add(new DynamicDataRoute("DD/{table}/{action}.aspx")
        {
            Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }),
            Model = model
        });

我的 List.aspx.cs 如下所示:

public partial class List : System.Web.UI.Page
{
    protected MetaTable table;

    protected void Page_Init(object sender, EventArgs e)
    {
        //DynamicDataManager1.RegisterControl(GridView1, true /*setSelectionFromUrl*/);
        table = GridDataSource.GetTable();
        DynamicDataManager1.RegisterControl(GridView1, true /*setSelectionFromUrl*/);
        GridView1.ColumnsGenerator = new AdvancedFieldGenerator(table, true);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        table = GridDataSource.GetTable();
        Title = table.DisplayName;
        GridDataSource.Include = table.ForeignKeyColumnsNames;
        InsertHyperLink.NavigateUrl = table.GetActionPath(PageAction.Insert);

        // Disable various options if the table is readonly
        if (table.IsReadOnly)
        {
            GridView1.Columns[0].Visible = false;
            InsertHyperLink.Visible = false;
        }
    }

    protected void OnFilterSelectedIndexChanged(object sender, EventArgs e)
    {
        GridView1.PageIndex = 0;
    }
}

...我正在使用 Dynamic Data Futures 的一个版本获得列排序、更好的验证等功能,这些功能运行良好。 对(例如)List.aspx.cs(如上所示)进行了一些调整,并更改了日期格式,以便新西兰风格的日期在我的美国网络服务器上工作。 除此之外,一切都很标准,AFAIK。

我的 EF 模型位于一个单独的程序集中,并且(显然)使用部分类扩展了一些实体。 我只想显示两个计算出的血腥字段,但完全没有成功。 我感觉自己就像一个新程序员,正在用头撞砖墙——所有的老招数都不起作用。 编程不应该这么难:-(

有人,任何人,请帮忙!!

伯纳德。

... it just doesn't work, at all. I've tried for days, with all different combinations of frick'n stuff and it won't budge.

There are certainly people out there who seem to be blogging about breezing through this sort of thing without seeing a glimpse of an issue saying things like "We all know that you can show public properties of extended EF classes by..." and "If you want to extend your data model to show a calculated field, simply..." - not so simple for me - arghghhhhghhhhhghh!!!

So, as per all of the typical examples, my EF partial class looks like this:

[DisplayColumn("Name")]
[MetadataType(typeof(SaleProduct_Metadata))]
public partial class SaleProduct
{        
    public string Test
    {
        get
        {
            return "blah";
        }
    }

    public class SaleProduct_Metadata
    {
        [ScaffoldColumn(true)] 
        public string Test;
    }
}  

My global.asax looks like this:

        MetaModel model = new MetaModel();
        model.RegisterContext(typeof(Sale.Models.SaleEntities), new ContextConfiguration() { ScaffoldAllTables = true });
        routes.Add(new DynamicDataRoute("DD/{table}/{action}.aspx")
        {
            Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }),
            Model = model
        });

And my List.aspx.cs looks like this:

public partial class List : System.Web.UI.Page
{
    protected MetaTable table;

    protected void Page_Init(object sender, EventArgs e)
    {
        //DynamicDataManager1.RegisterControl(GridView1, true /*setSelectionFromUrl*/);
        table = GridDataSource.GetTable();
        DynamicDataManager1.RegisterControl(GridView1, true /*setSelectionFromUrl*/);
        GridView1.ColumnsGenerator = new AdvancedFieldGenerator(table, true);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        table = GridDataSource.GetTable();
        Title = table.DisplayName;
        GridDataSource.Include = table.ForeignKeyColumnsNames;
        InsertHyperLink.NavigateUrl = table.GetActionPath(PageAction.Insert);

        // Disable various options if the table is readonly
        if (table.IsReadOnly)
        {
            GridView1.Columns[0].Visible = false;
            InsertHyperLink.Visible = false;
        }
    }

    protected void OnFilterSelectedIndexChanged(object sender, EventArgs e)
    {
        GridView1.PageIndex = 0;
    }
}

...I'm using a version of Dynamic Data Futures to get functionality like column ordering, better validation, etc, which is working fine. Have made a couple of adjustments to (e.g.) List.aspx.cs (shown above) and have changed the date formatting so as NZ style dates work on my US web-server. Other than that, everything's pretty standard, AFAIK.

My EF Models are housed in a separate assembly and am (obviously) extending some of the Entitys using partial classes. I just want to show two calculated bloody fields, but am having no success at all. I feel like I'm a new programmer bashing my head against a brick wall - none of the old tricks work. Programming shouldn't be this hard :-(

Someone, anyone, please help!!

Bernard.

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

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

发布评论

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

评论(2

獨角戲 2024-08-05 05:42:56

我不确定,但如果您使用的是 EF 3.5 SP1...

这意味着您的类派生自 EntityObject,我怀疑动态数据是这种情况的特殊情况并使用自定义的 TypeDescriptionProvider 知道如何从ObjectContext获取EF元数据,然后伪造必要的属性。

可能是问题的原因,因为TypeDescriptionProvider可能只使用EF知道的属性的元数据。 这当然不包括您计算的属性。

我知道这不是一个“答案”,但希望它能为您指明正确的方向。

亚历克斯

I'm not sure but if you are using EF 3.5 SP1...

that would mean your classes derive from EntityObject, I suspect that Dynamic Data is special casing this situation and using a custom TypeDescriptionProvider that know how to get EF metadata from the ObjectContext, and then fakes the necessary Attributes.

This might be the cause of the problem, because maybe that TypeDescriptionProvider is only using the metadata for properties EF knows about. Which of course excludes your calculated properties.

I know this isn't an 'answer' but hopefully it points you in the right direction.

Alex

好倦 2024-08-05 05:42:56

使用 EF 时,分部类的命名空间必须与为您生成的分部类的命名空间相匹配。 在 Linq-2-SQL 版本中,生成的分部类没有命名空间,因此它可以正常工作。
http://forums.asp.net/t/1473192.aspx

When using EF, the namespaces of your partial class must match the namespace of the partial class generated for you. In Linq-2-SQL versions, the generated partial classes do not have a namespace, so it just works.
http://forums.asp.net/t/1473192.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文