LINQ to SQL不更新MVC 2应用程序中的数据库记录

时间:2010-07-02 02:25:24

标签: database linq-to-sql asp.net-mvc-2 repository-pattern updating

使用MVC 2我一直在努力创建这个记录存储项目。 创建记录工作但更新它们不起作用。也没有抛出异常。

我在submitchanges()之前检查了getchangeset(),它显示了全部为零。

感谢您的阅读并帮助您:)

编辑视图:

<% 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...

编辑功能

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")
        }
    }

编辑帖子功能

[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;
        }
    }

这就是saveSong()在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();
    }

[情境事物]

这是上下文库

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; }
    }
}

例如在歌曲控制器中

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

        songRepository = new SqlSongRepository(contextRepository.connectionString);
    }

然后使用我之前发布的songRepository。

2 个答案:

答案 0 :(得分:0)

为什么不试试

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();
}

编辑:

以下是我如何从我的存储库更新我的Users表的示例(对不起,这是在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

然后在我的控制器中,我决定是否要添加或更新。

答案 1 :(得分:0)

好吧,解决了这个问题。改变

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

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

做了这个伎俩。

我不知道为什么,如果有人可以解释那就太好了。

谢谢!

相关问题