开发者

Null values in entity when entitydatasource Deleting Event raised

I'm currently using the Entity Framework and I have a Gridview displaying a list of records from a database. I have a Remove button the uses the Delete command. Each record has a file on the server associated with it, so when the data source raises the deleting event I want to grab the 开发者_C百科filename and delete the file from the server as well. The weird thing is that in my ds_Deleting event some of the values in the entity are null. I can't seem to figure out why.

The code for my Delete button in the gridview is the following:

<asp:TemplateField HeaderText="Remove">
    <ItemTemplate>
        <asp:Button ID="btnRemove" runat="server" Text="Remove" CssClass="button_default" CommandName="Delete" OnClientClick="return confirm('Deleting this contract will also delete the file from the server. Continue?')" />
     </ItemTemplate>
 </asp:TemplateField>

The OnDeleting event in the codebehind looks like this:

protected void dsContracts_Deleting(object sender, EntityDataSourceChangingEventArgs e)
{
    ipiModel.Contract contract = (ipiModel.Contract)e.Entity;

    File.Delete(Path.Combine(ConfigurationManager.AppSettings["ContractUploadPath"], contract.FileName));
}

Every time the contract.FileName value is null, even though it is displayed properly in the GridView. Any help would be much appreciated. Thanks!


I managed to figure this out and decided I'd write it down here in case anyone else has the same problem. I checked the documentation on MSDN for the deleting event of the entity data source (which can be found here) and it said

The Entity property of the EntityDataSourceChangingEventArgs object is used to access the object to be deleted. The properties of this object may not be fully set. Only the properties required to identify the object must be set.

So this is why I was getting null values. The solution I came up with probably isn't ideal but it works. I realized the primary key ContractID always had a value so I used it to pull the record from the database. Here is my code:

protected void dsContracts_Deleting1(object sender, EntityDataSourceChangingEventArgs e)
{
    ipiModel.Contract ct = (ipiModel.Contract)e.Entity;

    using (var db = new ipiModel.ipiEntities())
    {
        var contract = db.Contracts.Where(c => c.ContractID == ct.ContractID).Single();

        File.Delete(Path.Combine(ConfigurationManager.AppSettings["ContractUploadPath"], contract.FileName));
    }
}


Having experienced this with an ASP.NET Dynamic Data List page, and given that there appears to be a bug with varchar foreign keys (accepted by Microsoft I believe), any solution would be something of a workaround (hack).

In order to work around null values for table columns with default values (e.g. createdDate, CreatedBy etc), I've ended up with setting default values in the constructor of a partial class for the entity concerned.

After the information above gave the clue that only the columns required to uniquely identify the entity are loaded, I found that adding another 'default' value for the problem (foreign key) column fixed my issue - i.e. because I don't have any cascade delete or anything else, the primary key of the entity is enough to do the delete, and it's other processing that's checking for null values in the foreign key (varchar) - so I simply set that column to String.Empty in the contructor, and the default value I set is ignored...

e.g.

public partial class MyEntity
{

    public MyEntity()
    {
        CreatedDate = Datetime.Now;
        //Other default stuff
        MyProblemVarcharForeignKeyField = String.Empty;
    }
}

et voila!


All you have to do is to Bind the property you want to access in the code behind to an object.

You could add a TemplateField with Hidden objects and Bind the values you want to use to them like this :

<asp:TemplateField HeaderText="Remove">
<ItemTemplate>
    <asp:HiddenField ID="HiddenField1" runat="server" value='<%# Bind("FileName") %>'/>
 </ItemTemplate>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜