开发者

LINQ to SQL not updating database records in MVC 2 application

Using MVC 2 I have been trying to make this record store project. Creating records work but updating them doesn't. No exceptions are thrown either.

I examined getchangeset() right before submitchanges() it shows all zeros.

Thanks for your reading and you help :)

The Edit view:

<% using (Html.BeginForm("Edit", "Song")) { %>
    <fieldset>          
            <%: Html.HiddenFor(model => model.SongID) %>
            <%: Html.HiddenFor(model => model.DownloadCount) %>
            <%: Html.HiddenFor(model => model.Rating) %>
            <%: Html.HiddenFor(model => model.TagMapID) %>

        <div class="editor-label">
            <%= Html.LabelFor(model => model.AlbumID) %>
        </div>
        <div class="editor-field">
            <%= Html.DropDownListFor(model => model.AlbumID, DataHelper.getAlbumList(Model.AlbumID)) %>
            <%= Html.ValidationMessageFor(model => model.AlbumID) %>
        </div>

        <div class="editor-label">
            <%= Html.LabelFor(model => model.FullName) %>
        </div>
        <div class="editor-field">
            <%= Html.TextBoxFor(model => model.FullName)%>
            <%= Html.ValidationMessageFor(model => model.FullName) %>
and so on...

Edit function

public ActionResult Edit(int id)
    {
        try
        {
            var model = songRepository.Song.Single(rec => rec.SongID == id);

            return View(model);
        }
        catch (Exception anyEx)
        {
            throw anyEx;
            //return View("Error")
        }
    }

Edit post function

[AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Edit(Song model)
    {
        try
        {
            if (ModelState.IsValid)
            {
                songRepository.SaveSong(model);
                TempData["adminmsg"] = "Song saved";
                return RedirectToAction("List", "Song");
            }
            else
            {
                TempData["adminmsg"] = "Song not saved";
                return View("Edit", model);
            }
        }
        catch (Exception anyEx)
        {
            throw anyEx;
        }
    }

This is what saveSong() looks like in SqlSongRepository

public void SaveSong(Song song)
    {
        if (song.SongID == 0)
        {
            songTable.InsertOnSubmit(song);
        }
     开发者_如何转开发   else
        {
            songTable.Attach(song);
            songTable.Context.Refresh(RefreshMode.KeepChanges, song);
        }

        songTable.Context.SubmitChanges();
    }

[Context things]

This is the context repository

public class ContextRepository : IContextRepository
{
    private string connStr;

    public ContextRepository(string connectionString)
    {
        this.connStr = connectionString;
    }

    public DataContext MasterContext
    {
        get { return new DataContext(connectionString); }
    }

    public string connectionString
    {
        get { return connStr; }
    }
}

In the song controller for example

public SongController(IContextRepository contextRepository)
    {
        this.contextRepository = contextRepository;
        MasterContext = this.contextRepository.MasterContext;
        DataHelper.InitContext(contextRepository);

        songRepository = new SqlSongRepository(contextRepository.connectionString);
    }

Then the songRepository is used as I posted before.


why not try

public void SaveSong(Song song)
{
    if (song.SongID == 0)
    {
        context.songTable.InsertOnSubmit(song); //adds as song
    }
    else
    {
        var _song = (from s in context.songTable
                   where s.ID == song.id
                   select s).Single();
        _song = song; //updates existing song
    }

    context.songTable.SubmitChanges();
}

EDIT:

Here's an example of how I update my Users table from my Repository (sorry it's in VB)

Public Class UserRepository : Implements IUserRepository
    Private dc As MyDBDataContext
    Public Sub New()
        dc = New MyDBDataContext
    End Sub

   Public Sub AddUser(ByVal openid As OpenID) Implements IUserRepository.AddUser
        Dim user As New User
        user.MemberSince = DateTime.Now
        openid.User = user

        dc.OpenIDs.InsertOnSubmit(openid)
    End Sub

    Public Sub UpdateUser(ByVal user As User) Implements IUserRepository.UpdateUser
        Dim _user = (From u In dc.Users
            Where u.ID = user.ID
            Select u).Single

        With _user
            .About = user.About
            .BirthDate = user.BirthDate
            .Email = user.Email
            .isClosed = user.isClosed
            .isProfileComplete = user.isProfileComplete
            .RegionID = user.RegionID
            .Reputation = user.Reputation
            .UserName = user.UserName
            .WebSite = user.WebSite
        End With

    End Sub
End Class

Then in my controller I decide if I'm adding or updating.


Well, solved the problem. Changing

songTable.Context.Refresh(RefreshMode.KeepChanges, song);

to

songTable.Context.Refresh(RefreshMode.KeepCurrentValues, song);

did the trick.

I don't know why, it would be great if someone could explain.

Thanks!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜