代码之家  ›  专栏  ›  技术社区  ›  kowsheek

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

  •  0
  • kowsheek  · 技术社区  · 16 年前

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

    编辑post函数

    [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 回复  |  直到 16 年前
        1
  •  0
  •   Chase Florell    16 年前

    为什么不试试

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

    编辑:

    以下是我如何从存储库更新用户表的示例(抱歉,它在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
    

    然后在控制器中,我决定是添加还是更新。

        2
  •  0
  •   kowsheek    16 年前

    嗯,解决了这个问题。改变

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

    到

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

    做了这个把戏。

    我不知道为什么,如果有人能解释就好了。

    谢谢!